Deadlocks caused by improper multiprocessing API usage Medium

Invoking improper multiprocessing APIs with the wrong parameters might lead to a deadlock. The deadlock can result when the child process generates enough output to block the OS pipe buffer and waits for it to accept more data.

Detector ID
python/multiprocessing-deadlock-prevention@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-

Noncompliant example

1def deadlock_prevention_noncompliant():
2    from subprocess import Popen, PIPE
3    process = Popen('sh ~/example.sh', stdout=PIPE)
4    # Noncompliant: uses the 'Popen.wait' with 'stdout=PIPE' or 'stderr=PIPE',
5    # resulting in a potential deadlock and busy loop.
6    process.wait()
7    print(process.returncode)

Compliant example

1def deadlock_prevention_compliant():
2    from subprocess import Popen, PIPE
3    process = Popen('sh ~/example.sh', stdout=PIPE)
4    # Compliant: uses 'Popen.communicate' method, avoiding a
5    # potential deadlock and busy loop.
6    process.communicate()[0]
7    print(process.returncode)