virtio_net: handle unexpected tx completion without panicking#2778
Draft
benhillis wants to merge 1 commit intomicrosoft:mainfrom
Draft
virtio_net: handle unexpected tx completion without panicking#2778benhillis wants to merge 1 commit intomicrosoft:mainfrom
benhillis wants to merge 1 commit intomicrosoft:mainfrom
Conversation
complete_tx_packet used .unwrap() on the pending packet lookup, which would panic if the backend returned a TxId that was out of range or already completed. Use get_mut().and_then() and log an error instead.
Contributor
There was a problem hiding this comment.
Pull request overview
Updates virtio_net TX completion handling to avoid panics when the network backend reports an unexpected TxId, improving robustness at the backend/device boundary.
Changes:
- Replace
unwrap()onpending_tx_packetslookup with a safeget_mut(...).and_then(...)pattern. - Log and ignore unexpected TX completions instead of panicking.
Comment on lines
+998
to
+1005
| let Some(mut tx_packet) = state | ||
| .pending_tx_packets | ||
| .get_mut(id.0 as usize) | ||
| .and_then(|p| p.take()) | ||
| else { | ||
| tracing::error!(tx_id = id.0, "unexpected tx completion for unknown packet"); | ||
| return Ok(()); | ||
| }; |
There was a problem hiding this comment.
The log message here says "unknown packet", but this branch also triggers when the TxId is in range and the slot is already None (e.g., duplicate completion). Consider distinguishing the cases (out-of-range vs already-completed) and/or including the pending_tx_packets length in the log fields to make debugging misbehaving backends easier.
Suggested change
| let Some(mut tx_packet) = state | |
| .pending_tx_packets | |
| .get_mut(id.0 as usize) | |
| .and_then(|p| p.take()) | |
| else { | |
| tracing::error!(tx_id = id.0, "unexpected tx completion for unknown packet"); | |
| return Ok(()); | |
| }; | |
| let idx = id.0 as usize; | |
| let pending_len = state.pending_tx_packets.len(); | |
| let Some(slot) = state.pending_tx_packets.get_mut(idx) else { | |
| tracing::error!( | |
| tx_id = id.0, | |
| tx_index = idx, | |
| pending_tx_packets_len = pending_len, | |
| "unexpected tx completion for out-of-range packet" | |
| ); | |
| return Ok(()); | |
| }; | |
| let Some(mut tx_packet) = slot.take() else { | |
| tracing::error!( | |
| tx_id = id.0, | |
| tx_index = idx, | |
| pending_tx_packets_len = pending_len, | |
| "unexpected tx completion for already-completed packet" | |
| ); | |
| return Ok(()); | |
| }; |
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.
complete_tx_packet used .unwrap() on the pending packet lookup, which would panic if the backend returned a TxId that was out of range or already completed. Use get_mut().and_then() and log an error instead.