Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't panic on unknown token_id in gRPC callbacks. #210

30 changes: 20 additions & 10 deletions src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,16 @@ impl Dispatcher {
}

fn on_grpc_receive_initial_metadata(&self, token_id: u32, headers: u32) {
let context_id = *self
.grpc_streams
.borrow_mut()
.get(&token_id)
.expect("invalid token_id");
let grpc_streams_ref = self.grpc_streams.borrow_mut();
let context_id_hash_slot = grpc_streams_ref.get(&token_id);
let context_id = match context_id_hash_slot {
Some(id) => *id,
None => {
// TODO: change back to a panic once underlying issue is fixed.
PiotrSikora marked this conversation as resolved.
Show resolved Hide resolved
trace!("on_grpc_receive_initial_metadata: invalid token_id");
PiotrSikora marked this conversation as resolved.
Show resolved Hide resolved
return;
}
};

if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) {
self.active_id.set(context_id);
Expand Down Expand Up @@ -485,11 +490,16 @@ impl Dispatcher {
}
PiotrSikora marked this conversation as resolved.
Show resolved Hide resolved

fn on_grpc_receive_trailing_metadata(&self, token_id: u32, trailers: u32) {
let context_id = *self
.grpc_streams
.borrow_mut()
.get(&token_id)
.expect("invalid token_id");
let grpc_streams_ref = self.grpc_streams.borrow_mut();
let context_id_hash_slot = grpc_streams_ref.get(&token_id);
let context_id = match context_id_hash_slot {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be written as:

let context_id = match self.grpc_streams.borrow_mut().get(&token_id) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason I thought the rust compiler would stop me. Checked again though, it works fine, thanks for the tip

Some(id) => *id,
None => {
// TODO: change back to a panic once underlying issue is fixed.
trace!("on_grpc_receive_trailing_metadata: invalid token_id");
return;
}
};

if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) {
self.active_id.set(context_id);
Expand Down
Loading