@@ -33,13 +33,17 @@ struct Request
33
33
# ignoring other fields
34
34
shutdown:: Bool
35
35
end
36
+ is_shutdown (r:: Request ) = r. shutdown
36
37
37
38
# worker executes Request and returns a serialized Response object *if* Request has an id
38
39
struct Response
39
40
result
40
41
error:: Union{Nothing, Exception}
41
42
id:: UInt64 # matches a corresponding Request.id
43
+ # if true, worker is shutting down, so we can stop listening to it.
44
+ shutdown:: Bool
42
45
end
46
+ is_shutdown (r:: Response ) = r. shutdown
43
47
44
48
# simple Future that coordinator can wait on until a Response comes back for a Request
45
49
struct Future
@@ -232,6 +236,7 @@ function process_responses(w::Worker, ev::Threads.Event)
232
236
# get the next Response from the worker
233
237
r = deserialize (w. socket)
234
238
@assert r isa Response " Received invalid response from worker $(w. pid) : $(r) "
239
+ is_shutdown (r) && break
235
240
# println("Received response $(r) from worker $(w.pid)")
236
241
@lock lock begin
237
242
@assert haskey (reqs, r. id) " Received response for unknown request $(r. id) from worker $(w. pid) "
@@ -318,7 +323,14 @@ function serve_requests(io)
318
323
while true
319
324
req = deserialize (io)
320
325
@assert req isa Request
321
- req. shutdown && break
326
+ if is_shutdown (req)
327
+ resp = Response (nothing , nothing , rand (UInt64), true )
328
+ @lock iolock begin
329
+ # println("sending response: $(resp)")
330
+ serialize (io, resp)
331
+ flush (io)
332
+ end
333
+ end
322
334
# println("received request: $(req)")
323
335
Threads. @spawn begin
324
336
r = $ req
0 commit comments