scryer-prolog icon indicating copy to clipboard operation
scryer-prolog copied to clipboard

socket_server_accept/4 cannot be interrupted with Ctrl-c

Open triska opened this issue 5 years ago • 7 comments

Currently, I get:

?- use_module(library(sockets)).
   true.
?- socket_server_open(7014, Socket),
   socket_server_accept(Socket, Client, Stream, [type(binary)]).
^C^C^C^C^Z

Ideally, socket_server_accept/4 can also be interrupted, much like read/1.

triska avatar May 10 '20 17:05 triska

This might require a transition to mio or some other asynchronous I/O crate.

mthom avatar May 10 '20 18:05 mthom

This might require a transition to mio or some other asynchronous I/O crate.

I think Tokio would be more appropriate, mio is really the low-level library on top of which Tokio is built. This is an issue I can take a look at I think!

matt2xu avatar May 15 '20 23:05 matt2xu

@matt2xu : Thank you a lot, I would greatly appreciate this!

triska avatar May 26 '20 21:05 triska

what would be the relevant areas to modify to accomodate this? I can find some http server definitions here, for example.

razetime avatar Sep 09 '22 02:09 razetime

@matt2xu, @razetime: The most important section for this is in system_calls.rs, specifically in:

https://github.com/mthom/scryer-prolog/blob/de10ccfdeea33abb5d64d5a4bc33ee39b14ab002/src/machine/system_calls.rs#L6136

For comparison, the HTTP server already uses tokio:

https://github.com/mthom/scryer-prolog/blob/de10ccfdeea33abb5d64d5a4bc33ee39b14ab002/src/machine/system_calls.rs#L4323

triska avatar Jul 16 '23 12:07 triska

Can the ideas from #1998 be used to make this also work for socket_server_accept/4? (@aarroyoc ?)

triska avatar Sep 27 '23 19:09 triska

Technically the same idea could be used. On tcp_listener.accept() do something like a timeout to check if while waiting for a connection, a Ctrl-C has happened. But the TcpListener API doesn't provide an easy way to check for timeouts. They do have a non_blocking mode which will just return an error if there's nothing there, but putting that on a loop isn't good for the CPU. Tokio's TcpListener doesn't provide an accept_timeout method either but since it returns a future I think it can be combined with tokio timeouts to achieve the goal.

aarroyoc avatar Sep 27 '23 20:09 aarroyoc