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

Skip to content

Commit e88a244

Browse files
committed
Issue #15646: Prevent equivalent of a fork bomb when using multiprocessing
on Windows without the "if __name__ == '__main__'" idiom.
1 parent 296d1be commit e88a244

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
@@ -331,7 +331,7 @@ def get_command_line():
331331
'''
332332
Returns prefix of command line used for spawning a child process
333333
'''
334-
if process.current_process()._identity==() and is_forking(sys.argv):
334+
if getattr(process.current_process(), '_inheriting', False):
335335
raise RuntimeError('''
336336
Attempt to start a new process before the current process
337337
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
@@ -18,6 +18,7 @@
1818
import random
1919
import logging
2020
import test.support
21+
import test.script_helper
2122

2223

2324
# Skip tests if _multiprocessing wasn't built.
@@ -2429,9 +2430,29 @@ def test_timeout(self):
24292430
finally:
24302431
socket.setdefaulttimeout(old_timeout)
24312432

2433+
#
2434+
# Test what happens with no "if __name__ == '__main__'"
2435+
#
2436+
2437+
class TestNoForkBomb(unittest.TestCase):
2438+
def test_noforkbomb(self):
2439+
name = os.path.join(os.path.dirname(__file__), 'mp_fork_bomb.py')
2440+
if WIN32:
2441+
rc, out, err = test.script_helper.assert_python_failure(name)
2442+
self.assertEqual('', out.decode('ascii'))
2443+
self.assertIn('RuntimeError', err.decode('ascii'))
2444+
else:
2445+
rc, out, err = test.script_helper.assert_python_ok(name)
2446+
self.assertEqual('123', out.decode('ascii').rstrip())
2447+
self.assertEqual('', err.decode('ascii'))
2448+
2449+
#
2450+
#
2451+
#
2452+
24322453
testcases_other = [OtherTest, TestInvalidHandle, TestInitializers,
24332454
TestStdinBadfiledescriptor, TestInvalidFamily,
2434-
TestTimeouts]
2455+
TestTimeouts, TestNoForkBomb]
24352456

24362457
#
24372458
#

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ Core and Builtins
101101
Library
102102
-------
103103

104+
- Issue #15646: Prevent equivalent of a fork bomb when using
105+
multiprocessing on Windows without the "if __name__ == '__main__'"
106+
idiom.
107+
104108
- Issue #15424: Add a __sizeof__ implementation for array objects.
105109
Patch by Ludwig Hähne.
106110

0 commit comments

Comments
 (0)