浏览代码

Fix reusing IDs, fix closing the server event stream, apply rustfmt.

父节点
当前提交
67eaf1057c
共有 1 个文件被更改,包括 12 次插入5 次删除
  1. 12
    5
      src/network/client.rs

+ 12
- 5
src/network/client.rs 查看文件

63
     ErrorType: fmt::Debug,
63
     ErrorType: fmt::Debug,
64
 {
64
 {
65
     let mut free_ids = IdAllocator::new();
65
     let mut free_ids = IdAllocator::new();
66
-    let mut calls_in_progress = BTreeMap::<u32, oneshot::Sender<Result<PacketType, RPCError<ErrorType>>>>::new();
66
+    let mut calls_in_progress =
67
+        BTreeMap::<u32, oneshot::Sender<Result<PacketType, RPCError<ErrorType>>>>::new();
67
 
68
 
68
     // TODO: Refactor this function.
69
     // TODO: Refactor this function.
69
 
70
 
82
                                     // We received a response to a call - find the call and forward
83
                                     // We received a response to a call - find the call and forward
83
                                     // the reply.
84
                                     // the reply.
84
                                     if let Some(call) = calls_in_progress.remove(&deserialized.id) {
85
                                     if let Some(call) = calls_in_progress.remove(&deserialized.id) {
86
+                                        free_ids.free(deserialized.id);
85
                                         call.send(Ok(deserialized.payload)).ok();
87
                                         call.send(Ok(deserialized.payload)).ok();
86
                                     } else {
88
                                     } else {
87
                                         // TODO: Use proper logging functionality.
89
                                         // TODO: Use proper logging functionality.
88
-                                        eprintln!("Received a reply for an unknown call ({:?})!", deserialized.id);
90
+                                        eprintln!(
91
+                                            "Received a reply for an unknown call ({:?})!",
92
+                                            deserialized.id
93
+                                        );
89
                                     }
94
                                     }
90
                                 }
95
                                 }
91
                             }
96
                             }
159
         };
164
         };
160
     }
165
     }
161
 
166
 
167
+    drop(event_send);
168
+
162
     // Return errors for all pending packets as there will be no responses.
169
     // Return errors for all pending packets as there will be no responses.
163
     for (_, result) in calls_in_progress {
170
     for (_, result) in calls_in_progress {
164
         result.send(Err(RPCError::Closed)).ok();
171
         result.send(Err(RPCError::Closed)).ok();
224
 impl<PacketType, ErrorType> Drop for RPCInterface<PacketType, ErrorType> {
231
 impl<PacketType, ErrorType> Drop for RPCInterface<PacketType, ErrorType> {
225
     fn drop(&mut self) {
232
     fn drop(&mut self) {
226
         // Stop the connection loop and close the connection.
233
         // Stop the connection loop and close the connection.
227
-        if let Err(_) = self.cmd_send.send(ClientTaskCommand::Close) {
228
-            panic!("could not send close signal to connection task");
229
-        }
234
+        // send() only fails if the other end has already been closed - we must
235
+        // not unwrap() or expect() because then nested panics might occur.
236
+        self.cmd_send.send(ClientTaskCommand::Close).ok();
230
     }
237
     }
231
 }
238
 }
232
 
239
 

正在加载...
取消
保存