Skip to content

Commit 8fcdb54

Browse files
committed
extmod/modlwip: Fix close and clean up of UDP and raw sockets.
The correct callback-deregister functions must be called dependent on the socket type, otherwise resources may not be freed correctly. Signed-off-by: Damien George <[email protected]>
1 parent 90d47ee commit 8fcdb54

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

extmod/modlwip.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,16 +1502,16 @@ STATIC mp_uint_t lwip_socket_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_
15021502
return 0;
15031503
}
15041504

1505-
// Deregister callback (pcb.tcp is set to NULL below so must deregister now)
1506-
tcp_arg(socket->pcb.tcp, NULL);
1507-
tcp_err(socket->pcb.tcp, NULL);
1508-
tcp_recv(socket->pcb.tcp, NULL);
1509-
15101505
// Free any incoming buffers or connections that are stored
15111506
lwip_socket_free_incoming(socket);
15121507

15131508
switch (socket->type) {
15141509
case MOD_NETWORK_SOCK_STREAM: {
1510+
// Deregister callback (pcb.tcp is set to NULL below so must deregister now)
1511+
tcp_arg(socket->pcb.tcp, NULL);
1512+
tcp_err(socket->pcb.tcp, NULL);
1513+
tcp_recv(socket->pcb.tcp, NULL);
1514+
15151515
if (socket->pcb.tcp->state != LISTEN) {
15161516
// Schedule a callback to abort the connection if it's not cleanly closed after
15171517
// the given timeout. The callback must be set before calling tcp_close since
@@ -1525,10 +1525,12 @@ STATIC mp_uint_t lwip_socket_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_
15251525
break;
15261526
}
15271527
case MOD_NETWORK_SOCK_DGRAM:
1528+
udp_recv(socket->pcb.udp, NULL, NULL);
15281529
udp_remove(socket->pcb.udp);
15291530
break;
15301531
#if MICROPY_PY_LWIP_SOCK_RAW
15311532
case MOD_NETWORK_SOCK_RAW:
1533+
raw_recv(socket->pcb.raw, NULL, NULL);
15321534
raw_remove(socket->pcb.raw);
15331535
break;
15341536
#endif

tests/multi_net/udp_data.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Simple test of a UDP server and client transferring data
2+
3+
import socket
4+
5+
NUM_NEW_SOCKETS = 4
6+
NUM_TRANSFERS = 4
7+
PORT = 8000
8+
9+
# Server
10+
def instance0():
11+
multitest.globals(IP=multitest.get_network_ip())
12+
multitest.next()
13+
for i in range(NUM_NEW_SOCKETS):
14+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
15+
s.bind(socket.getaddrinfo("0.0.0.0", PORT)[0][-1])
16+
multitest.broadcast("server ready")
17+
for j in range(NUM_TRANSFERS):
18+
data, addr = s.recvfrom(1000)
19+
print(data)
20+
s.sendto(b"server to client %d %d" % (i, j), addr)
21+
s.close()
22+
23+
24+
# Client
25+
def instance1():
26+
multitest.next()
27+
ai = socket.getaddrinfo(IP, PORT)[0][-1]
28+
for i in range(NUM_NEW_SOCKETS):
29+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
30+
multitest.wait("server ready")
31+
for j in range(NUM_TRANSFERS):
32+
s.sendto(b"client to server %d %d" % (i, j), ai)
33+
data, addr = s.recvfrom(1000)
34+
print(data)
35+
s.close()

0 commit comments

Comments
 (0)