8000 extmod/modlwip: Register TCP close-timeout callback before closing PCB. · devmonkZA/micropython@019dd84 · GitHub
[go: up one dir, main page]

Skip to content

Commit 019dd84

Browse files
committed
extmod/modlwip: Register TCP close-timeout callback before closing PCB.
In d5f0c87 this call to tcp_poll() was added to put a timeout on closing TCP sockets. But after calling tcp_close() the PCB may be freed and therefore invalid, so tcp_poll() can not be used at that point. As a fix this commit calls tcp_poll() before closing the TCP PCB. If the PCB is subsequently closed and freed by tcp_close() or tcp_abort() then the PCB will not be on any active list and the callback will not be executed, which is the desired behaviour (the _lwip_tcp_close_poll() callback only needs to be called if the PCB remains active for longer than the timeout).
1 parent 734ada3 commit 019dd84

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

extmod/modlwip.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,12 +1421,15 @@ STATIC mp_uint_t lwip_socket_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_
14211421

14221422
switch (socket->type) {
14231423
case MOD_NETWORK_SOCK_STREAM: {
1424+
if (socket->pcb.tcp->state != LISTEN) {
1425+
// Schedule a callback to abort the connection if it's not cleanly closed after
1426+
// the given timeout. The callback must be set before calling tcp_close since
1427+
// the latter may free the pcb; if it doesn't then the callback will be active.
1428+
tcp_poll(socket->pcb.tcp, _lwip_tcp_close_poll, MICROPY_PY_LWIP_TCP_CLOSE_TIMEOUT_MS / 500);
1429+
}
14241430
if (tcp_close(socket->pcb.tcp) != ERR_OK) {
14251431
DEBUG_printf("lwip_close: had to call tcp_abort()\n");
14261432
tcp_abort(socket->pcb.tcp);
1427-
} else {
1428-
// If connection not cleanly closed after timeout then abort the connection
1429-
tcp_poll(socket->pcb.tcp, _lwip_tcp_close_poll, MICROPY_PY_LWIP_TCP_CLOSE_TIMEOUT_MS / 500);
14301433
}
14311434
break;
14321435
}

0 commit comments

Comments
 (0)
0