Socket Sleep and Wakeup clarification

Mark Johnston markj at freebsd.org
Wed Oct 16 17:33:38 UTC 2019


On Wed, Jul 31, 2019 at 04:36:08PM -0400, Dheeraj Kandula wrote:
> Hi All,
>       I am reading through the socket code in uipc_socket.c file of FreeBSD
> 12.
> 
> The code invokes wakeup with the channel as so->so_timeo in the following
> functions:
> soisconnected
> soisdisconnected
> soisdisconnecting and
> soshutdown
> 
> The callers of soconnect invoke sleep so that the thread that invokes
> soconnect wakes up when the TCP 3 way handshake is done. The soconnect in
> kernel returns immediately unlike user space connect which sleeps.
> 
> I also see tsleep in soclose when the socket's state is SS_ISCONNECTED.
> 
> My questions:
>        1. Is it possible to close a socket when the application is sleeping
> after the application invokes soconnect. Basically I am trying to figure
> out how multiple threads can access the same socket for soconnect and
> soclose to happen at the same time.

I don't see any particular synchronization between soclose() and the
sleep you are referring to, so there might be a bug here.  In
particular, I would expect soclose() on a connecting socket to wake up
sleepers, but I cannot see how that happens.

>         2. soshutdown also invokes wakeup. This wakeup again corresponds to
> the sleep by soconnect. Isn't it? How can we have two threads accessing the
> same socket with one thread sleeping on a socket for the soconnect, while
> another shuts down the same socket in either the RD or WR or RW direction.

Yes, wakeup(&so->so_timeo) should be called whenever the connection
state of a socket changes.


More information about the freebsd-net mailing list