Skip to content

TCP socket in passive mode can't handle a packet when the buffer's non-empty #34

Closed
eriksoe opened this Issue Nov 10, 2010 · 2 comments

2 participants

@eriksoe
eriksoe commented Nov 10, 2010

To reproduce:
{ok, P} = gen_tcp:listen(12321, [binary, {active,false}]).
{ok,Q} = gen_tcp:accept(P).
Telnet to port 12321.
Press return twice.

Observed result:
Port goes into a busy-loop; with inet debugging enabled, this is visible from the debug output.

Code analysis:
TCPInet.tcp_recv() handles the "socket is passive, buffer is no-empty" case badly; it does not read any input, nor does it disable interest.
In the above case,
i_remain==0, nread==0, lenp[0]==2 (for "\r\n")
which makes tcp_recv() try to deliver data (and return) without having read any new data. Hence the input remains ready.

@krestenkrab
Trifork member

This is what I observe:

1> {ok, P} = gen_tcp:listen(12321, [binary, {active,false}]).
{ok,port:542}
2> {ok,Q} = gen_tcp:accept(P).
sock_select TCPIP@3061481[sock=InetServerSocket[sun.nio.ch.ServerSocketChannelImpl[/0.0.0.0:12321] open=true blocking=false addr=/0.0.0.0:12321]; state=AcBoConCogLiMuOp; active=PASSIVE; deliver=1; select=10000; nointrest] ops=10000; mode=SET

%% now I telnet to the port

readyAccept TCPIP@3061481[sock=InetServerSocket[sun.nio.ch.ServerSocketChannelImpl[/0.0.0.0:12321] open=true blocking=false addr=/0.0.0.0:12321]; state=AcBoConCogLiMuOp; active=PASSIVE; deliver=1; select=10000; nointrest]
sock_select TCPIP@3061481[sock=InetServerSocket[sun.nio.ch.ServerSocketChannelImpl[/0.0.0.0:12321] open=true blocking=false addr=/0.0.0.0:12321]; state=AcBoConCogLiMuOp; active=PASSIVE; deliver=1; select=0; nointrest] ops=10000; mode=CLEAR
{ok,port:550}
3>

%% doesn't that look OK?

When I then enter \n\n in the telnet shell, nothing happens because it is not actively selecting.

@eriksoe
eriksoe commented Nov 11, 2010

Never mind - I'd accidentally reenabled these lines:
// accept_desc.select(sock.channel(), ERL_DRV_READ,
// SelectMode.SET);

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.