Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3165a75

Browse files
committed
Merge 3.2
2 parents e471772 + e88a244 commit 3165a75

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

Lib/multiprocessing/forking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def get_command_line():
305305
'''
306306
Returns prefix of command line used for spawning a child process
307307
'''
308-
if process.current_process()._identity==() and is_forking(sys.argv):
308+
if getattr(process.current_process(), '_inheriting', False):
309309
raise RuntimeError('''
310310
Attempt to start a new process before the current process
311311
has finished its bootstrapping phase.

Lib/test/mp_fork_bomb.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import multiprocessing, sys
2+
3+
def foo():
4+
print("123")
5+
6+
# Because "if __name__ == '__main__'" is missing this will not work
7+
# correctly on Windows. However, we should get a RuntimeError rather
8+
# than the Windows equivalent of a fork bomb.
9+
10+
p = multiprocessing.Process(target=foo)
11+
p.start()
12+
p.join()
13+
sys.exit(p.exitcode)

Lib/test/test_multiprocessing.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import logging
2121
import struct
2222
import test.support
23+
import test.script_helper
2324

2425

2526
# Skip tests if _multiprocessing wasn't built.
@@ -3346,9 +3347,29 @@ def test_timeout(self):
33463347
finally:
33473348
socket.setdefaulttimeout(old_timeout)
33483349

3350+
#
3351+
# Test what happens with no "if __name__ == '__main__'"
3352+
#
3353+
3354+
class TestNoForkBomb(unittest.TestCase):
3355+
def test_noforkbomb(self):
3356+
name = os.path.join(os.path.dirname(__file__), 'mp_fork_bomb.py')
3357+
if WIN32:
3358+
rc, out, err = test.script_helper.assert_python_failure(name)
3359+
self.assertEqual('', out.decode('ascii'))
3360+
self.assertIn('RuntimeError', err.decode('ascii'))
3361+
else:
3362+
rc, out, err = test.script_helper.assert_python_ok(name)
3363+
self.assertEqual('123', out.decode('ascii').rstrip())
3364+
self.assertEqual('', err.decode('ascii'))
3365+
3366+
#
3367+
#
3368+
#
3369+
33493370
testcases_other = [OtherTest, TestInvalidHandle, TestInitializers,
33503371
TestStdinBadfiledescriptor, TestWait, TestInvalidFamily,
3351-
TestFlags, TestTimeouts]
3372+
TestFlags, TestTimeouts, TestNoForkBomb]
33523373

33533374
#
33543375
#

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- Issue #15646: Prevent equivalent of a fork bomb when using
17+
multiprocessing on Windows without the "if __name__ == '__main__'"
18+
idiom.
19+
1620
C API
1721
-----
1822

0 commit comments

Comments
 (0)