On Mon, 11 Mar 2019 14:25:10 GMT, David Eckardt wrote:
Following up on my other post, my main task reads from a TCP socket and needs to be interrupted on a signal:
void main() { bool do_shutdown; auto t = runTask({ auto conn = connectTCP("localhost", 4711); auto recvbuf = new ubyte[12345]; while (!do_shutdown) { conn.read(recvbuf); logInfo("Still alive..."); } }); eventDriver.signals.listen(SIGINT, (id, status, sig) { auto l = yieldLock(); eventDriver.signals.releaseRef(id); logInfo("Caught SIGINT!"); runTask({ logInfo("Initiating shutdown procedure..."); // Cancel the read() call in the other task t.join(); logInfo("Shut down!"); exitEventLoop(); }); }); runApplication(); }
How do I cancel the
read
call during a shutdown? I foundeventcore.socket.cancelRead
, which accepts aref StreamSocket
argument, and I am not sure how to pass aTCPSocket
.
You can do a t.interrupt()
call, which causes an InterruptException
to be thrown from read
(which internally calls cancelRead
).
BTW, there should also be a try-catch around the whole task function, which catches this exception, as well as others that may be thrown by connectTCP
or read
, but currently uncaught exceptions will just terminate the task, so in this case that may be okay.