Skip to content

Commit ecfff63

Browse files
authored
bpo-40280: Disable AF_UNIX, AF_PACKET, SO_REUSE* on Emscripten (#31829)
Emscripten's socket emulation is limited. AF_UNIX, AF_PACKET, setsockopt(), and most SO_* constants are not supported.
1 parent 3b128c0 commit ecfff63

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

Lib/socketserver.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,9 @@ def server_bind(self):
465465
May be overridden.
466466
467467
"""
468-
if self.allow_reuse_address:
468+
if self.allow_reuse_address and hasattr(socket, "SO_REUSEADDR"):
469469
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
470-
if self.allow_reuse_port:
470+
if self.allow_reuse_port and hasattr(socket, "SO_REUSEPORT"):
471471
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
472472
self.socket.bind(self.server_address)
473473
self.server_address = self.socket.getsockname()

Modules/socketmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7933,7 +7933,7 @@ PyInit__socket(void)
79337933
#ifdef IPPROTO_VRRP
79347934
PyModule_AddIntMacro(m, IPPROTO_VRRP);
79357935
#endif
7936-
#if defined(IPPROTO_SCTP) && !defined(__EMSCRIPTEN__)
7936+
#ifdef IPPROTO_SCTP
79377937
PyModule_AddIntMacro(m, IPPROTO_SCTP);
79387938
#endif
79397939
#ifdef IPPROTO_BIP

Modules/socketmodule.h

+15
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ typedef int socklen_t;
192192

193193
#endif /* HAVE_SOCKADDR_ALG */
194194

195+
#ifdef __EMSCRIPTEN__
196+
// wasm32-emscripten sockets only support subset of IPv4 and IPv6.
197+
// SCTP protocol crashes runtime.
198+
#ifdef IPPROTO_SCTP
199+
# undef IPPROTO_SCTP
200+
#endif
201+
// setsockopt() fails with ENOPROTOOPT, getsockopt only supports SO_ERROR.
202+
// undef SO_REUSEADDR and SO_REUSEPORT so they cannot be used.
203+
#ifdef SO_REUSEADDR
204+
# undef SO_REUSEADDR
205+
#endif
206+
#ifdef SO_REUSEPORT
207+
# undef SO_REUSEPORT
208+
#endif
209+
#endif // __EMSCRIPTEN__
195210

196211
#ifndef Py__SOCKET_H
197212
#define Py__SOCKET_H

Tools/wasm/config.site-wasm32-emscripten

+4-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ ac_cv_func_posix_fallocate=no
7474
ac_cv_func_utimensat=no
7575
ac_cv_header_sys_ioctl_h=no
7676

77-
# sockets are supported, but only in non-blocking mode
78-
# ac_cv_header_sys_socket_h=no
77+
# sockets are supported, but only AF_INET / AF_INET6 in non-blocking mode.
78+
# Disable AF_UNIX and AF_PACKET support, see socketmodule.h.
79+
ac_cv_header_sys_un_h=no
80+
ac_cv_header_netpacket_packet_h=no
7981

8082
# aborts with bad ioctl
8183
ac_cv_func_openpty=no

0 commit comments

Comments
 (0)