Mercurial > public > mercurial-scm > hg
comparison mercurial/worker.py @ 49269:395f28064826
worker: avoid potential partial write of pickled data
Previously, the code wrote the pickled data using os.write(). However,
os.write() can write less bytes than passed to it. To trigger the problem, the
pickled data had to be larger than 2147479552 bytes on my system.
Instead, open a file object and pass it to pickle.dump(). This also has the
advantage that it doesn?t buffer the whole pickled data in memory.
Note that the opened file must be buffered because pickle doesn?t support
unbuffered streams because unbuffered streams? write() method might write less
bytes than passed to it (like os.write()) but pickle.dump() relies on that all
bytes are written (see https://github.com/python/cpython/issues/93050).
The side effect of using a file object and a with statement is that wfd is
explicitly closed now while it seems like before it was implicitly closed by
process exit.
author | Manuel Jacob <me@manueljacob.de> |
---|---|
date | Sun, 22 May 2022 03:50:34 +0200 |
parents | 520722523955 |
children | 311fcc5a65f6 |
comparison
equal
deleted
inserted
replaced
49268:7b0cf4517d82 | 49269:395f28064826 |
---|---|
248 def workerfunc(): | 248 def workerfunc(): |
249 for r, w in pipes[:-1]: | 249 for r, w in pipes[:-1]: |
250 os.close(r) | 250 os.close(r) |
251 os.close(w) | 251 os.close(w) |
252 os.close(rfd) | 252 os.close(rfd) |
253 for result in func(*(staticargs + (pargs,))): | 253 with os.fdopen(wfd, 'wb') as wf: |
254 os.write(wfd, pickle.dumps(result)) | 254 for result in func(*(staticargs + (pargs,))): |
255 pickle.dump(result, wf) | |
256 wf.flush() | |
255 return 0 | 257 return 0 |
256 | 258 |
257 ret = scmutil.callcatch(ui, workerfunc) | 259 ret = scmutil.callcatch(ui, workerfunc) |
258 except: # parent re-raises, child never returns | 260 except: # parent re-raises, child never returns |
259 if os.getpid() == parentpid: | 261 if os.getpid() == parentpid: |