You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 5, 2019. It is now read-only.
<5> Finally, we parse each line into a list of destination logins and the message itself.
217
217
218
+
== Managing Errors
219
+
220
+
One serious problem in the above solution is that, while we correctly propagate errors in the `client`, we just drop the error on the floor afterwards!
221
+
That is, `task::spawn` does not return error immediately (it can't, it needs to run the future to completion first), only after it is joined.
222
+
We can "fix" it by waiting for the task to be joined, like this:
223
+
224
+
[source,rust]
225
+
----
226
+
let handle = task::spawn(client(stream)); <1>
227
+
handle.await?
228
+
----
229
+
230
+
The `.await` waits until the client finishes, and `?` propagates the result.
231
+
232
+
There are two problems with this solution however!
233
+
_First_, because we immediately await the client, we can only handle one client at time, and that completely defeats the purpose of async!
234
+
_Second_, if a client encounters an IO error, the whole server immediately exits.
235
+
That is, a flaky internet connection of one peer brings down the whole chat room!
236
+
237
+
A correct way to handle client errors in this case is log them, and continue serving other clients.
0 commit comments