This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Safe A-Sync Socket Operations

Hi,

I was wondering if I could get some guidance (or directed to some documentation on the subject) with regards to what socket operations are safe to do in a-sync timer callback.

Right now I am trying the following:

  1. I setup a UDP socket listener.
  2. I create a timer whose handler checks every n seconds for any incoming data (specifically it uses recvfrom() with the MSG_PEEK flag).
  3. If there is any incoming data I then read the full incoming data and then send, to a different UDP server, a packet which includes a copy of that incoming data.

If I try and do step 3 asynchronously, within the timer handler, it fails.

The steps involve  include:

  • Reading the incoming UDP data from the existing UDP socket listener (this works fine).
  • Calling getaddrinfo() (this fails)
  • Creating a new socket with socket() (this fails)
  • Sending the UDP packet with sendto() (this fails)

By failing I mean that the code gets stuck and doesn't complete. No faults or other errors are seen in the debug output.
Additionally, adding a debug print to the main loop shows that the main loop is not continuing either.

My current solution is to handle this synchronously by raising a flag at the end of step 2, then executing step 3 in the main loop if said flag is raised.

However, I am curious about what is going on here. I imagine that there are other callbacks (possibly using the same timer interface) which are being delayed or blocked by my timer callback. Where can I find more on detailed information on this?

Parents Reply Children
No Data
Related