@@ -121,6 +121,21 @@ def is_abstract_socket_namespace(address):
121
121
# Function returning a temp directory which will be removed on exit
122
122
#
123
123
124
+ # Maximum length of a socket file path is usually between 92 and 108 [1].
125
+ # BSD-based operating systems usually use 104 (OpenBSD, FreeBSD, macOS)
126
+ # and Linux uses 108 [2].
127
+ #
128
+ # [1]: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_un.h.html
129
+ # [2]: https://man7.org/linux/man-pages/man7/unix.7.html.
130
+
131
+ if sys .platform == 'linux' :
132
+ _SUN_PATH_MAX = 104
133
+ elif sys .platform .startswith (('openbsd' , 'freebsd' )):
134
+ _SUN_PATH_MAX = 108
135
+ else :
136
+ # On Windows platforms, we do not create AF_UNIX sockets.
137
+ _SUN_PATH_MAX = None if os .name == 'nt' else 92
138
+
124
139
def _remove_temp_dir (rmtree , tempdir ):
125
140
rmtree (tempdir )
126
141
@@ -135,7 +150,26 @@ def get_temp_dir():
135
150
tempdir = process .current_process ()._config .get ('tempdir' )
136
151
if tempdir is None :
137
152
import shutil , tempfile
138
- tempdir = tempfile .mkdtemp (prefix = 'pymp-' )
153
+ if os .name == 'nt' :
154
+ tempdir = tempfile .mkdtemp (prefix = 'pymp-' )
155
+ else :
156
+ # Most of the time, the root temporary directory is /tmp, and thus
157
+ # listener sockets files "$TMPDIR/pymp-XXXXXXXX/sock-XXXXXXXX"
158
+ # do not have a path length exceeding SUN_PATH_MAX.
159
+ #
160
+ # If users specify their own temporary directory, we may be unable
161
+ # to create those files. Therefore, we fall back to the system-wide
162
+ # temporary directory /tmp, assumed to exist on POSIX systems.
163
+ #
164
+ # See https://github.com/python/cpython/issues/132124.
165
+ base_tempdir = tempfile .gettempdir ()
166
+ # len(base_tempdir) + len('/pymp-XXXXXXXX') + len('/sock-XXXXXXXX')
167
+ sun_path_len = len (base_tempdir ) + 14 + 14
168
+ if sun_path_len > _SUN_PATH_MAX :
169
+ # fallback to the system-wide temporary directory,
170
+ # ignoring environment variables.
171
+ base_tempdir = '/tmp'
172
+ tempdir = tempfile .mkdtemp (prefix = 'pymp-' , dir = base_tempdir )
139
173
info ('created temp directory %s' , tempdir )
140
174
# keep a strong reference to shutil.rmtree(), since the finalizer
141
175
# can be called late during Python shutdown
0 commit comments