Commit graph

436 commits

Author SHA1 Message Date
luccabb
3d1f8cae48
[3.12] gh-88887: Cleanup multiprocessing.resource_tracker.ResourceTracker upon deletion (GH-130429) (#131530)
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
(cherry picked from commit f53e7de6a8)
2025-03-21 11:15:35 +01:00
Miss Islington (bot)
ddb314f598
[3.12] remove type annotations from multiprocessing. (GH-129381) (#129471)
remove type annotations from multiprocessing. (GH-129381)

* remove type annotations from multiprocessing.

One of them was technically invalid per typing specs... but since we're not
checking these in the stdlib today lets elide them.

https://discuss.python.org/t/static-type-annotations-in-cpython/65068/13

* use the actual comment style annotation format



---------
(cherry picked from commit 71aecc284e)

Co-authored-by: Gregory P. Smith <greg@krypto.org>
Co-authored-by: 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) <wk.cvs.github@sydorenko.org.ua>
2025-01-30 09:08:31 +00:00
Miss Islington (bot)
be7314ffac
[3.12] gh-127586: multiprocessing.Pool does not properly restore blocked signals (try 2) (GH-128011) (#128299)
gh-127586: multiprocessing.Pool does not properly restore blocked signals (try 2) (GH-128011)

Correct pthread_sigmask in resource_tracker to restore old signals

Using SIG_UNBLOCK to remove blocked "ignored signals" may accidentally
cause side effects if the calling parent already had said signals
blocked to begin with and did not intend to unblock them when
creating a pool. Use SIG_SETMASK instead with the previous mask of
blocked signals to restore the original blocked set.
(cherry picked from commit aeb9b65aa2)

Co-authored-by: Stephen Hansen <stephen.paul.hansen@gmail.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
2024-12-29 11:02:59 -08:00
Miss Islington (bot)
f08c82ecad
[3.12] Add the missing f on an f-string error message in multiprocessing. (GH-127462) (#127465)
Add the missing `f` on an f-string error message in multiprocessing. (GH-127462)
(cherry picked from commit 11c01092d5)

Co-authored-by: Gregory P. Smith <greg@krypto.org>
2024-11-30 23:50:33 -08:00
Miss Islington (bot)
d0d892f7a3
[3.12] gh-71936: Fix race condition in multiprocessing.Pool (GH-124973) (GH-126870)
Proxes of shared objects register a Finalizer in BaseProxy._incref(), and it
will call BaseProxy._decref() when it is GCed. This may cause a race condition
with Pool(maxtasksperchild=None) on Windows.

A connection would be closed and raised TypeError when a GC occurs between
_ConnectionBase._check_writable() and _ConnectionBase._send_bytes() in
_ConnectionBase.send() in the second or later task, and a new object
is allocated that shares the id() of a previously deleted one.

Instead of using the id() of the token (or the proxy), use a unique,
non-reusable number.

(cherry picked from commit ba088c8f9c)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Akinori Hattori <hattya@gmail.com>
2024-11-15 14:04:35 +00:00
Alex Waygood
86efa2f1fa
[3.12] gh-126451: Revert backports of ABC registrations for contextvars.Context and multiprocessing proxies (#126735) 2024-11-12 12:26:23 +00:00
Gregory P. Smith
7bb92ed505
[3.12] gh-117378: Fix multiprocessing forkserver preload sys.path inheritance. (GH-126538) (GH-126633)
gh-117378: Fix multiprocessing forkserver preload sys.path inheritance.

`sys.path` was not properly being sent from the parent process when launching
the multiprocessing forkserver process to preload imports.  This bug has been
there since the forkserver start method was introduced in Python 3.4.  It was
always _supposed_ to inherit `sys.path` the same way the spawn method does.

Observable behavior change: A `''` value in `sys.path` will now be replaced in
the forkserver's `sys.path` with an absolute pathname
`os.path.abspath(os.getcwd())` saved at the time that `multiprocessing` was
imported in the parent process as it already was when using the spawn start
method. **This will only be observable during forkserver preload imports**.

The code invoked before calling things in another process already correctly sets `sys.path`.
Which is likely why this went unnoticed for so long as a mere performance issue in
some configurations.

A workaround for the bug on impacted Pythons is to set PYTHONPATH in the
environment before multiprocessing's forkserver process was started. Not perfect
as that is then inherited by other children, etc, but likely good enough for many
people's purposes.

(cherry picked from commit 9d08423b6e)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-11-10 00:13:26 +00:00
Miss Islington (bot)
4f10b8eacf
[3.12] gh-125679: multiprocessing Lock and RLock - fix invalid representation string on MacOSX. (GH-125680) (#126534)
gh-125679: multiprocessing Lock and RLock - fix invalid representation string on MacOSX. (GH-125680)
(cherry picked from commit 75f7cf91ec)

Co-authored-by: Duprat <yduprat@gmail.com>
2024-11-07 09:50:46 +00:00
Alex Waygood
47d48b62dd
[3.12] gh-126417: Register multiprocessing proxy types to an appropriate collections.abc class (#126419) (#126436)
Co-authored-by: Stephen Morton <github@tungol.org>
2024-11-05 11:19:45 +00:00
Miss Islington (bot)
3af945fbb4
[3.12] bpo-43952: Fix multiprocessing Listener authkey bug (GH-25845) (GH-115995)
Listener.accept() no longer hangs when authkey is an empty bytes object.
(cherry picked from commit 686ec17f50)

Co-authored-by: Miguel Brito <5544985+miguendes@users.noreply.github.com>
2024-02-27 16:13:53 +00:00
Petr Viktorin
c912bc3ed4
[3.12] gh-114440: Close writer pipe in multiprocessing.Queue, not concurrent.futures (GH-114489)
This was left out of the 3.12 backport for three related issues:
- gh-107219 (which adds `self.call_queue._writer.close()` to `_ExecutorManagerThread` in `concurrent.futures`)
- gh-109370 (which changes this to be only called on Windows)
- gh-109047 (which moves the call to `multiprocessing.Queue`'s `_terminate_broken`)

Without this change, ProcessPoolExecutor sometimes hangs on Windows
when a worker process is terminated.

Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-01-24 13:21:10 +01:00
Miss Islington (bot)
2aea0e967a
[3.12] gh-101225: Increase the socket backlog when creating a multiprocessing.connection.Listener (GH-113567) (#114018)
gh-101225: Increase the socket backlog when creating a multiprocessing.connection.Listener (GH-113567)

Increase the backlog for multiprocessing.connection.Listener` objects created
 by `multiprocessing.manager` and `multiprocessing.resource_sharer` to
 significantly reduce the risk of getting a connection refused error when creating
 a `multiprocessing.connection.Connection` to them.
(cherry picked from commit c7d59bd8cf)

Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
2024-01-14 16:29:15 +01:00
Miss Islington (bot)
1bff2fd013
[3.12] gh-113421: Fix multiprocessing logger for "%(filename)s" (GH-113423) (GH-113450)
(cherry picked from commit ce77ee5035)

Co-authored-by: Xu Song <xusong.vip@gmail.com>
2023-12-24 10:23:32 +00:00
Miss Islington (bot)
8c92d64ede
[3.12] gh-113009: Fix multiprocessing Process.terminate() on Windows (GH-113128) (#113177)
gh-113009: Fix multiprocessing Process.terminate() on Windows (GH-113128)

On Windows, Process.terminate() no longer sets the returncode
attribute to always call WaitForSingleObject() in Process.wait().
Previously, sometimes the process was still running after
TerminateProcess() even if GetExitCodeProcess() is not STILL_ACTIVE.
(cherry picked from commit 4026ad5b2c)

Co-authored-by: Victor Stinner <vstinner@python.org>
2023-12-15 15:21:02 +00:00
Serhiy Storchaka
7e82c626c4
[3.12] gh-79429: Ignore FileNotFoundError when remove a temporary directory in the multiprocessing finalizer (GH-112865) 2023-12-09 15:13:40 +02:00
Serhiy Storchaka
4018209fff
[3.12] gh-109370: Fix unexpected traceback output in test_concurrent_futures (GH-109780) (GH-111934)
Follow-up of gh-107219.

* Only close the connection writer on Windows.
* Also use existing constant _winapi.ERROR_OPERATION_ABORTED instead of
  WSA_OPERATION_ABORTED.
(cherry picked from commit 0b4e090422)
2023-11-10 07:17:02 +00:00
Victor Stinner
356de021d7
[3.12] gh-109047: concurrent.futures catches RuntimeError (#109810) (#110126)
gh-109047: concurrent.futures catches PythonFinalizationError (#109810)

concurrent.futures: The *executor manager thread* now catches
exceptions when adding an item to the *call queue*. During Python
finalization, creating a new thread can now raise RuntimeError. Catch
the exception and call terminate_broken() in this case.

Add test_python_finalization_error() to test_concurrent_futures.

concurrent.futures._ExecutorManagerThread changes:

* terminate_broken() no longer calls shutdown_workers() since the
  call queue is no longer working anymore (read and write ends of
  the queue pipe are closed).
* terminate_broken() now terminates child processes, not only
  wait until they complete.
* _ExecutorManagerThread.terminate_broken() now holds shutdown_lock
  to prevent race conditons with ProcessPoolExecutor.submit().

multiprocessing.Queue changes:

* Add _terminate_broken() method.
* _start_thread() sets _thread to None on exception to prevent
  leaking "dangling threads" even if the thread was not started
  yet.

(cherry picked from commit 6351842121)
2023-10-02 17:47:10 +02:00
Miss Islington (bot)
a2074911ba
[3.12] gh-110036: multiprocessing Popen.terminate() catches PermissionError (GH-110037) (#110064)
gh-110036: multiprocessing Popen.terminate() catches PermissionError (GH-110037)

On Windows, multiprocessing Popen.terminate() now catchs
PermissionError and get the process exit code. If the process is
still running, raise again the PermissionError. Otherwise, the
process terminated as expected: store its exit code.
(cherry picked from commit bd4518c60c)

Co-authored-by: Victor Stinner <vstinner@python.org>
2023-10-02 17:44:26 +02:00
Miss Islington (bot)
b723b8a13d
[3.12] gh-109593: Fix reentrancy issue in multiprocessing resource_tracker (GH-109629) (#109898)
gh-109593: Fix reentrancy issue in multiprocessing resource_tracker (GH-109629)

---------

(cherry picked from commit 0eb98837b6)

Co-authored-by: Antoine Pitrou <antoine@python.org>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
2023-10-02 17:37:40 +02:00
Miss Islington (bot)
1d8c18c39d
[3.12] gh-107219: Fix concurrent.futures terminate_broken() (GH-109244) (#109254)
gh-107219: Fix concurrent.futures terminate_broken() (GH-109244)

Fix a race condition in concurrent.futures. When a process in the
process pool was terminated abruptly (while the future was running or
pending), close the connection write end. If the call queue is
blocked on sending bytes to a worker process, closing the connection
write end interrupts the send, so the queue can be closed.

Changes:

* _ExecutorManagerThread.terminate_broken() now closes
  call_queue._writer.
* multiprocessing PipeConnection.close() now interrupts
  WaitForMultipleObjects() in _send_bytes() by cancelling the
  overlapped operation.
(cherry picked from commit a9b1f84790)

Co-authored-by: Victor Stinner <vstinner@python.org>
2023-10-02 16:56:09 +02:00
Miss Islington (bot)
320d398262
[3.12] gh-108520: Fix bad fork detection in nested multiprocessing use case (GH-108568) (#108691)
gh-108520: Fix bad fork detection in nested multiprocessing use case (GH-108568)

gh-107275 introduced a regression where a SemLock would fail being passed along nested child processes, as the `is_fork_ctx` attribute would be left missing after the first deserialization.

---------

(cherry picked from commit add8d45cbe)

Co-authored-by: albanD <desmaison.alban@gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Antoine Pitrou <pitrou@free.fr>
2023-08-30 23:18:49 +02:00
Miss Islington (bot)
9cf49aaaa0
[3.12] gh-77377: Ensure multiprocessing SemLock is valid for spawn-based Process before serializing it (GH-107275) (#108377)
gh-77377: Ensure multiprocessing SemLock is valid for spawn-based Process before serializing it (GH-107275)

Ensure multiprocessing SemLock is valid for spawn Process before serializing it.

Creating a multiprocessing SemLock with a fork context, and then trying to pass it to a spawn-created Process, would segfault if not detected early.

---------

(cherry picked from commit 1700d34d31)

Co-authored-by: albanD <desmaison.alban@gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Antoine Pitrou <pitrou@free.fr>
2023-08-24 01:44:07 +02:00
Miss Islington (bot)
c2fb25a714
[3.12] More actionable error message when spawn is incorrectly used. (GH-102203) (#107990)
More actionable error message when spawn is incorrectly used. (GH-102203)
(cherry picked from commit a794ebeb02)

Co-authored-by: Yuxin Wu <ppwwyyxxc@gmail.com>
Co-authored-by: Yuxin Wu <ppwwyyxx@users.noreply.github.com>
Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
2023-08-16 14:22:22 +02:00
Miss Islington (bot)
f0a583b6fb
[3.12] gh-107963: Fix set_forkserver_preload to check the type of given list (GH-107965) (#107975)
gh-107963: Fix set_forkserver_preload to check the type of given list (GH-107965)
(cherry picked from commit 6515ec3d3d)


gh-107963: Fix set_forkserver_preload to check the type of given list

Co-authored-by: Dong-hee Na <donghee.na@python.org>
2023-08-15 16:56:53 +02:00
Miss Islington (bot)
4787eaed06
[3.12] gh-90876: Restore the ability to import multiprocessing when sys.executable is None (GH-106464) (#106494)
gh-90876: Restore the ability to import multiprocessing when `sys.executable` is `None` (GH-106464)

Prevent `multiprocessing.spawn` from failing to *import* in environments
where `sys.executable` is `None`.  This regressed in 3.11 with the addition
of support for path-like objects in multiprocessing.

Adds a test decorator to have tests only run when part of test_multiprocessing_spawn to `_test_multiprocessing.py` so we can start to avoid re-running the same not-global-state specific test in all 3 modes when there is no need.
(cherry picked from commit c60df361ce)

Co-authored-by: Gregory P. Smith <greg@krypto.org>
2023-07-06 16:25:06 -07:00
Luccccifer
ef5d00a592
gh-104536: Improve multiprocessing.process._cleanup logic (#104537)
Fix a race condition in the internal `multiprocessing.process` cleanup
logic that could manifest as an unintended `AttributeError` when calling
`BaseProcess.close()`.

---------

Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
2023-05-22 03:48:57 +00:00
Christian Heimes
3ed57e4995
gh-61460: Stronger HMAC in multiprocessing (#20380)
bpo-17258:  `multiprocessing` now supports stronger HMAC algorithms for inter-process connection authentication rather than only HMAC-MD5.

Signed-off-by: Christian Heimes <christian@python.org>

gpshead: I Reworked to be more robust while keeping the idea.

The protocol modification idea remains, but we now take advantage of the
message length as an indicator of legacy vs modern protocol version.  No
more regular expression usage.  We now default to HMAC-SHA256, but do so
in a way that will be compatible when communicating with older clients
or older servers. No protocol transition period is needed.

More integration tests to verify these claims remain true are required. I'm
unaware of anyone depending on multiprocessing connections between
different Python versions.

---------

Signed-off-by: Christian Heimes <christian@python.org>
Co-authored-by: Gregory P. Smith [Google] <greg@krypto.org>
2023-05-20 23:33:09 +00:00
Gregory P. Smith
d4c410f0f9
gh-84559: Remove the new multiprocessing warning, too disruptive. (#101551)
This reverts the core of #100618 while leaving relevant documentation
improvements and minor refactorings in place.
2023-02-03 15:20:46 -08:00
Gregory P. Smith
0ca67e6313
GH-84559: Deprecate fork being the multiprocessing default. (#100618)
This starts the process. Users who don't specify their own start method
and use the default on platforms where it is 'fork' will see a
DeprecationWarning upon multiprocessing.Pool() construction or upon
multiprocessing.Process.start() or concurrent.futures.ProcessPool use.

See the related issue and documentation within this change for details.
2023-02-02 15:50:35 -08:00
Nikita Sobolev
ce39aaffee
gh-99509: Add __class_getitem__ to multiprocessing.queues.Queue (#99511) 2022-12-26 20:50:55 -08:00
Arne de Laat
a694b8222e
Fix typo in exception message in multiprocessing.pool (#99900) 2022-11-30 20:57:28 +05:30
Zackery Spytz
85c128e34d
bpo-40882: Fix a memory leak in SharedMemory on Windows (GH-20684)
In multiprocessing.shared_memory.SharedMemory(), the temporary view
returned by MapViewOfFile() should be unmapped when it is no longer
needed.
2022-11-25 17:39:48 +00:00
Gregory P. Smith
abf5b6ff43
gh-61460: Add a comment describing the multiprocessing.connection protocol (gh-99623)
Describe the multiprocessing connection protocol.

It isn't a good protocol, but it is what it is.  This way we can more
easily reason about making changes to it in a backwards compatible way.
2022-11-20 10:20:04 -08:00
Gregory P. Smith
49f61068f4
gh-97514: Don't use Linux abstract sockets for multiprocessing (#98501)
Linux abstract sockets are insecure as they lack any form of filesystem
permissions so their use allows anyone on the system to inject code into
the process.

This removes the default preference for abstract sockets in
multiprocessing introduced in Python 3.9+ via
https://github.com/python/cpython/pull/18866 while fixing
https://github.com/python/cpython/issues/84031.

Explicit use of an abstract socket by a user now generates a
RuntimeWarning.  If we choose to keep this warning, it should be
backported to the 3.7 and 3.8 branches.
2022-10-20 15:30:09 -07:00
Steve Dower
e48f9b2b7e
gh-98360: multiprocessing now spawns children on Windows with correct argv[0] in virtual environments (GH-98462) 2022-10-20 14:53:38 +01:00
Koki Saito
27e59afa2a
gh-97816: Remove unused variables in mutliprocessing.managers.Server (#97817)
Remove unused local variables.
2022-10-03 22:29:17 -07:00
Koki Saito
19ca114645
gh-96819: multiprocessing.resource_tracker: check if length of pipe write <= 512 (#96890)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
2022-10-02 17:41:01 -07:00
Irit Katriel
e37a158725
GH-83658: make multiprocessing.Pool raise an exception if maxtasksperchild is not None or a positive int (GH-93364)
Closes #83658.
2022-06-17 00:14:26 -07:00
samtygier
9a458befdd
gh-91577: SharedMemory move imports out of methods (#91579)
SharedMemory.unlink() uses the unregister() function from resource_tracker. Previously it was imported in the method, but this can fail if the method is called during interpreter shutdown, for example when unlink is part of a __del__() method.

Moving the import to the top of the file, means that the unregister() method is available during interpreter shutdown.

The register call in SharedMemory.__init__() can also use this imported resource_tracker.
2022-06-16 15:41:51 +02:00
Leo Trol
30610d2837
gh-90549: Fix leak of global named resources using multiprocessing spawn (#30617)
Co-authored-by: XD Trol <milestonejxd@gmail.com>
Co-authored-by: Antoine Pitrou <pitrou@free.fr>
2022-06-09 18:55:12 +02:00
Gregory P. Smith
f6dd14c653
gh-82616: Add process_group support to subprocess.Popen (#23930)
One more thing that can help prevent people from using `preexec_fn`.

Also adds conditional skips to two tests exposing ASAN flakiness on the Ubuntu 20.04 Address Sanitizer Github CI system. When that build is run on more modern systems the "problem" does not show up. It seems ASAN implementation related.

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2022-05-05 16:22:32 -07:00
Géry Ogam
dfb1b9da8a
bpo-47029: Fix BrokenPipeError in multiprocessing.Queue at garbage collection and explicit close (#31913) 2022-05-03 17:49:57 -06:00
Ma Lin
1699128c48
GH-91355: micro-optimize Connection.send_bytes() method (gh-32247) 2022-05-03 23:41:45 +09:00
Alexander Shadchin
f882d33778
Fix missing f prefix on f-strings (GH-91910) 2022-04-27 09:30:54 +03:00
Gregory P. Smith
cd5726fe67
gh-91401: Add a failsafe way to disable vfork. (#91490)
Just in case there is ever an issue with _posixsubprocess's use of
vfork() due to the complexity of using it properly and potential
directions that Linux platforms where it defaults to on could take, this
adds a failsafe so that users can disable its use entirely by setting
a global flag.

No known reason to disable it exists. But it'd be a shame to encounter
one and not be able to use CPython without patching and rebuilding it.

See the linked issue for some discussion on reasoning.

Also documents the existing way to disable posix_spawn.
2022-04-25 16:19:39 -07:00
Géry Ogam
5576ddbbbc
bpo-46720: Add support for path-like objects to multiprocessing.set_executable for Windows (GH-31279)
This bring the API to be on a par with Unix-like systems.
2022-04-22 15:47:09 -07:00
Victor Stinner
061a8bf77c
gh-91231: Add shutdown_timeout to multiprocessing BaseManager (#32112)
Add an optional keyword 'shutdown_timeout' parameter to the
multiprocessing.BaseManager constructor. Kill the process if
terminate() takes longer than the timeout.

Multiprocessing tests pass test.support.SHORT_TIMEOUT
to BaseManager.shutdown_timeout.
2022-04-19 16:27:00 +02:00
Nikita Sobolev
0cbdd21311
bpo-46565: del loop vars that are leaking into module namespaces (GH-30993) 2022-02-03 11:20:08 +02:00
Kumar Aditya
af6b406885
bpo-25066: Added repr for multiprocessing.Event (GH-29749)
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
2021-12-09 13:16:45 +00:00
Zackery Spytz
d097876111
bpo-38840: Incorrect __all__ in multiprocessing.managers (GH-18034)
This was causing test___all__ to fail on platforms lacking a shared
memory implementation.

Co-Authored-By: Xavier de Gaye <xdegaye@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
2021-08-09 18:44:55 +02:00