Quellcode durchsuchen

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

Mathias Gottschlag vor 5 Jahren
Ursprung
Commit
67eaf1057c
1 geänderte Dateien mit 12 neuen und 5 gelöschten Zeilen
  1. 12
    5
      src/network/client.rs

+ 12
- 5
src/network/client.rs Datei anzeigen

@@ -63,7 +63,8 @@ async fn client_connection_task<StreamType, ErrorType, PacketType>(
63 63
     ErrorType: fmt::Debug,
64 64
 {
65 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 69
     // TODO: Refactor this function.
69 70
 
@@ -82,10 +83,14 @@ async fn client_connection_task<StreamType, ErrorType, PacketType>(
82 83
                                     // We received a response to a call - find the call and forward
83 84
                                     // the reply.
84 85
                                     if let Some(call) = calls_in_progress.remove(&deserialized.id) {
86
+                                        free_ids.free(deserialized.id);
85 87
                                         call.send(Ok(deserialized.payload)).ok();
86 88
                                     } else {
87 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,6 +164,8 @@ async fn client_connection_task<StreamType, ErrorType, PacketType>(
159 164
         };
160 165
     }
161 166
 
167
+    drop(event_send);
168
+
162 169
     // Return errors for all pending packets as there will be no responses.
163 170
     for (_, result) in calls_in_progress {
164 171
         result.send(Err(RPCError::Closed)).ok();
@@ -224,9 +231,9 @@ impl<PacketType, ErrorType> RPCInterface<PacketType, ErrorType> {
224 231
 impl<PacketType, ErrorType> Drop for RPCInterface<PacketType, ErrorType> {
225 232
     fn drop(&mut self) {
226 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
 

Laden…
Abbrechen
Speichern