Return a typed error with raw bytes for malformed messages - #201
Open
xiaodongw wants to merge 1 commit into
Open
Return a typed error with raw bytes for malformed messages#201xiaodongw wants to merge 1 commit into
xiaodongw wants to merge 1 commit into
Conversation
When convertInMessage fails, ReadOp previously returned a plain
fmt.Errorf whose text discarded the offending message. That makes
kernel/protocol corruption hard to diagnose in production, since the
raw bytes are often the only useful signal.
Introduce MalformedMessageError, which carries the underlying
conversion error (exposed via Unwrap) plus a copy of the raw message
bytes, and return it from ReadOp. A new InMessage.Bytes() accessor
exposes the message read by the most recent Init so ReadOp can copy the
bytes before the buffer is recycled.
The error string is unchanged ("convertInMessage: <err>"), so existing
log output is preserved; callers that want the bytes can recover them
with errors.As.
Co-authored-by: Isaac
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
When a message read from
/dev/fusecan't be converted to an op,ReadOpreturns
fmt.Errorf("convertInMessage: %v", err). The conversion errors areopaque (
errors.New("Corrupt OpLookup")and friends — no opcode, length, orpayload), and the raw bytes of the offending message are recycled back into the
freelist before the caller ever sees them.
In production this makes malformed-message failures very hard to diagnose: the
only signal is which op parser failed, with no way to inspect what the kernel
actually sent.
What this changes
MalformedMessageError, returned fromReadOpwhenconvertInMessagefails. It wraps the underlying conversion error (via
Unwrap) and carries acopy of the raw message bytes in
Message, so callers can log/hex-dump them.InMessage.Bytes()ininternal/buffer, exposing the full message fromthe most recent
InitsoReadOpcan copy the bytes before the buffer isrecycled.
InMessageon the failure path viaputInMessage. Previouslythe buffer was only returned to the freelist by
Replyon the success path;the error path returns a
nilcontext, so the caller can't callReplyandthe buffer was orphaned. The copy is taken before recycling, so it stays
valid under concurrent readers.
Compatibility
"convertInMessage: <err>"), so existing logoutput and any string matching keep working.
fmt.Errorfto the exported*MalformedMessageError. Callers that want thebytes can recover them with
errors.As;errors.Is/Unwrapreach theoriginal conversion error.
Testing
TestMalformedMessageError— coversError(),Unwrap, anderrors.Asrecovery of the bytes through a wrapped error.
TestInMessageBytes— covers the new accessor.go build ./...,go vet, andgo test ./...pass.