-
Notifications
You must be signed in to change notification settings - Fork 227
Warn when multiprocessing start method is 'fork' #1309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9fd6b19
Warn when multiprocessing start method is 'fork'
Andy-Jost 476459f
Skip multiprocessing warning tests on Windows
Andy-Jost fbdd56d
Add reset_fork_warning function and rename check_multiprocessing_star…
Andy-Jost 3271964
Merge branch 'main' into warn-fork-multiprocessing
Andy-Jost File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """ | ||
| Test that warnings are emitted when multiprocessing start method is 'fork' | ||
| and IPC objects are serialized. | ||
|
|
||
| These tests use mocking to simulate the 'fork' start method without actually | ||
| using fork, avoiding the need for subprocess isolation. | ||
| """ | ||
|
|
||
| import warnings | ||
| from unittest.mock import patch | ||
|
|
||
| from cuda.core.experimental import DeviceMemoryResource, DeviceMemoryResourceOptions, EventOptions | ||
| from cuda.core.experimental._event import _reduce_event | ||
| from cuda.core.experimental._memory._ipc import ( | ||
| _deep_reduce_device_memory_resource, | ||
| _reduce_allocation_handle, | ||
| ) | ||
|
|
||
|
|
||
| def test_warn_on_fork_method_device_memory_resource(ipc_device): | ||
| """Test that warning is emitted when DeviceMemoryResource is pickled with fork method.""" | ||
| device = ipc_device | ||
| device.set_current() | ||
| options = DeviceMemoryResourceOptions(max_size=2097152, ipc_enabled=True) | ||
| mr = DeviceMemoryResource(device, options=options) | ||
|
|
||
| with patch("multiprocessing.get_start_method", return_value="fork"), warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
|
|
||
| # Reset the warning flag to allow testing | ||
| from cuda.core.experimental._utils import cuda_utils | ||
|
|
||
| cuda_utils._fork_warning_emitted = False | ||
|
|
||
| # Trigger the reduction function directly | ||
| _deep_reduce_device_memory_resource(mr) | ||
|
|
||
| # Check that warning was emitted | ||
| assert len(w) == 1, f"Expected 1 warning, got {len(w)}: {[str(warning.message) for warning in w]}" | ||
| warning = w[0] | ||
| assert warning.category is UserWarning | ||
| assert "fork" in str(warning.message).lower() | ||
| assert "spawn" in str(warning.message).lower() | ||
| assert "undefined behavior" in str(warning.message).lower() | ||
|
|
||
| mr.close() | ||
|
|
||
|
|
||
| def test_warn_on_fork_method_allocation_handle(ipc_device): | ||
| """Test that warning is emitted when IPCAllocationHandle is pickled with fork method.""" | ||
| device = ipc_device | ||
| device.set_current() | ||
| options = DeviceMemoryResourceOptions(max_size=2097152, ipc_enabled=True) | ||
| mr = DeviceMemoryResource(device, options=options) | ||
| alloc_handle = mr.get_allocation_handle() | ||
|
|
||
| with patch("multiprocessing.get_start_method", return_value="fork"), warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
|
|
||
| # Reset the warning flag to allow testing | ||
| from cuda.core.experimental._utils import cuda_utils | ||
|
|
||
| cuda_utils._fork_warning_emitted = False | ||
|
|
||
| # Trigger the reduction function directly | ||
| _reduce_allocation_handle(alloc_handle) | ||
|
|
||
| # Check that warning was emitted | ||
| assert len(w) == 1 | ||
| warning = w[0] | ||
| assert warning.category is UserWarning | ||
| assert "fork" in str(warning.message).lower() | ||
|
|
||
| mr.close() | ||
|
|
||
|
|
||
| def test_warn_on_fork_method_event(mempool_device): | ||
| """Test that warning is emitted when Event is pickled with fork method.""" | ||
| device = mempool_device | ||
| device.set_current() | ||
| stream = device.create_stream() | ||
| ipc_event_options = EventOptions(ipc_enabled=True) | ||
| event = stream.record(options=ipc_event_options) | ||
|
|
||
| with patch("multiprocessing.get_start_method", return_value="fork"), warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
|
|
||
| # Reset the warning flag to allow testing | ||
| from cuda.core.experimental._utils import cuda_utils | ||
|
|
||
| cuda_utils._fork_warning_emitted = False | ||
|
|
||
| # Trigger the reduction function directly | ||
| _reduce_event(event) | ||
|
|
||
| # Check that warning was emitted | ||
| assert len(w) == 1 | ||
| warning = w[0] | ||
| assert warning.category is UserWarning | ||
| assert "fork" in str(warning.message).lower() | ||
|
|
||
| event.close() | ||
|
|
||
|
|
||
| def test_no_warning_with_spawn_method(ipc_device): | ||
| """Test that no warning is emitted when start method is 'spawn'.""" | ||
| device = ipc_device | ||
| device.set_current() | ||
| options = DeviceMemoryResourceOptions(max_size=2097152, ipc_enabled=True) | ||
| mr = DeviceMemoryResource(device, options=options) | ||
|
|
||
| with patch("multiprocessing.get_start_method", return_value="spawn"), warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
|
|
||
| # Reset the warning flag to allow testing | ||
| from cuda.core.experimental._utils import cuda_utils | ||
|
|
||
| cuda_utils._fork_warning_emitted = False | ||
|
|
||
| # Trigger the reduction function directly | ||
| _deep_reduce_device_memory_resource(mr) | ||
|
|
||
| # Check that no fork-related warning was emitted | ||
| fork_warnings = [warning for warning in w if "fork" in str(warning.message).lower()] | ||
| assert len(fork_warnings) == 0, f"Unexpected warning: {fork_warnings[0].message if fork_warnings else None}" | ||
|
|
||
| mr.close() | ||
|
|
||
|
|
||
| def test_warning_emitted_only_once(ipc_device): | ||
| """Test that warning is only emitted once even when multiple objects are pickled.""" | ||
| device = ipc_device | ||
| device.set_current() | ||
| options = DeviceMemoryResourceOptions(max_size=2097152, ipc_enabled=True) | ||
| mr1 = DeviceMemoryResource(device, options=options) | ||
| mr2 = DeviceMemoryResource(device, options=options) | ||
|
|
||
| with patch("multiprocessing.get_start_method", return_value="fork"), warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
|
|
||
| # Reset the warning flag to allow testing | ||
| from cuda.core.experimental._utils import cuda_utils | ||
|
|
||
| cuda_utils._fork_warning_emitted = False | ||
|
|
||
| # Trigger reduction multiple times | ||
| _deep_reduce_device_memory_resource(mr1) | ||
| _deep_reduce_device_memory_resource(mr2) | ||
|
|
||
| # Check that warning was emitted only once | ||
| fork_warnings = [warning for warning in w if "fork" in str(warning.message).lower()] | ||
| assert len(fork_warnings) == 1, f"Expected 1 warning, got {len(fork_warnings)}" | ||
|
|
||
| mr1.close() | ||
| mr2.close() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of caching it, would it be better to always call
multiprocessing.get_start_method()and check it? I worry that it is a global state that we don't own and it could be changed at arbitrary point in time by the user or any package.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the official
multiprocessinglibrary docs, the description ofmultiprocessing.set_start_method()explains that:set_start_method()again without forcing will raise aRuntimeErrorindicating that the context has already been set.forceargument, but this is not part of the public, documented API and should be avoided in normal code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, Andy. Good to know Python by default checks this. However, I do see the
forceargument being documented, so it is part of the public API. And it does allow overwriting:If this is not on the hot path, I think not caching the result and always checking is safer.
Alternatively, we could mention in the warning message that setting
set_start_method(..., force=True) is dangerous because we cannot help capture issues.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more thing, if we already warned the next
warn()call is a no-op so adding the_fork_warning_emittedguard is redundant 😆There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see, you want to limit the warning to only once per process lifetime, regardless of which object raises the warning from. NVM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On minor detail -- since
set_start_methodcan only be set once, I don't think we need to check it multiple times. In other words, we can set_fork_warning_emitted = Trueunconditionally rather than only when a warning is emitted so we don't check again. And maybe renaming the flag to_fork_warning_checkedfor clarity. Unless I'm missing some case where it might be set exceptionally late and it does need to be checked multiple times.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comments. I've made adjustments to the name and conditionality as Mike suggested. I left this as a one-time check because there does not seem to be a strong consensus, but I don't feel strongly about that and wouldn't mind changing it if more discussion goes in that direction.
Incidentally, a separate warning is issued when fork is called in multithreaded programs (like those using CUDA), so if a user somehow sidesteps this warning by setting the start method multiple times, they will still get another nasty message indicating their program is invalid.