nonblocking option for net-accept?

Q&A's, tips, howto's

nonblocking option for net-accept?

Postby TedWalther » Wed Dec 05, 2018 10:46 pm

Lutz, can we have a non-blocking option for a listening socket, so I can accept and also handle socket session without doing a spawn/sync dance? I think net-select and net-peek already let me do what is needed to multiplex multiple connections in one process, just need a non-blocking net-accept.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.
TedWalther
 
Posts: 602
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC

Re: nonblocking option for net-accept?

Postby TedWalther » Thu Dec 06, 2018 4:17 am

The use case for this is I want to accept connections and keep them alive and process them in a round robin way while still accepting new connections. Otherwise it gets annoying trying to let all the different incoming connections interact with each other and a common shared state.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.
TedWalther
 
Posts: 602
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC

Re: nonblocking option for net-accept?

Postby TedWalther » Thu Dec 06, 2018 4:29 am

I did look at prodcons.lsp in the examples directory, and it doesn't look like it can do the job, because the server won't know the semaphore value for the individual connections that come in, assuming I fork for each connection and copied the producer/consumer code.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.
TedWalther
 
Posts: 602
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC

Re: nonblocking option for net-accept?

Postby Lutz » Thu Dec 06, 2018 4:36 pm

Lutz
 
Posts: 5258
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: nonblocking option for net-accept?

Postby TedWalther » Fri Dec 07, 2018 12:42 am

Lutz wrote:try to work with net-listen:

http://www.newlisp.org/downloads/newlis ... net-listen


Lutz, I read through that. Did I miss something?

My use case is:

Code: Select all
;; pseudocode
(net-listen port "nonblocking")
(set 'active-connections (list))

;; event loop
(while true
  (set 'newclient (net-accept)) ;; non-blocking
  (if newclient (push newclient active-connections))
  (do-something-with (net-select active-connections))
  (close-and-remove-inactive-connections)
)


I want to have multiple persistent connections in the server, but without them being blocked waiting for a new connection. I don't know a way to net-peek for a new and incoming connection to accept, so allowing the non-blocking option seems the easiest way. And return nil if there is no incoming connection to return.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.
TedWalther
 
Posts: 602
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC

Re: nonblocking option for net-accept?

Postby Lutz » Fri Dec 07, 2018 1:27 am

Why does net-select not work for you? Maybe I don't understand the question.
Lutz
 
Posts: 5258
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: nonblocking option for net-accept?

Postby TedWalther » Fri Dec 07, 2018 2:21 am

Lutz wrote:Why does net-select not work for you? Maybe I don't understand the question.


One learns something new every day. Thank you Lutz.

Whether for straight read, or for accept, without setting nonblocking mode on the socket, select() isn't guaranteed to give you back a socket that will return a result right away, it can still block.

I just did a search about using select to do a non-blocking accept, and found this on stack overflow:

Hmm, that's a well-known race condition - the accept(2) will block if client drops connection attempt between two syscalls. You need the listening socket to be non-blocking. – Nikolai Fetissov Aug 10 '10 at 0:35

This is correct - you can add your listening file descriptor to the readfds in your select() call, and select() will tell you the file descriptor is "readable" if it has a connection ready to accept(). @Nikolai is correct too - the listening socket should be nonblocking and the accept() call prepared to handle EAGAIN. – caf Aug 10 '10 at 0:35


Needs to handle EAGAIN and also EWOULDBLOCK as the potential errno.

So it would still be good to be able to set O_NONBLOCK on a socket at the net-listen stage.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.
TedWalther
 
Posts: 602
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC

Re: nonblocking option for net-accept?

Postby TedWalther » Fri Dec 07, 2018 3:15 am

Another quote, from here: https://jameshfisher.com/2017/04/05/set ... cking.html

If our server only makes calls which select has indicated will not block, will everything be OK? No! These two operations - select followed by the hopefully non-blocking call - are non-atomic. By the time the server makes the call, the situation may have changed! A pending connection may disappear before we try to accept it. A client attempting to send data may disappear before we try to read its data. Data may be read from a socket by a different process before we get to it.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.
TedWalther
 
Posts: 602
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 1 guest