Skip to content

multiprocessing.process._children is not multithread-safe, BaseProcess.close() function would cause AttributeError #104536

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

Closed
z764969689 opened this issue May 16, 2023 · 0 comments · Fixed by #104537 or gtopper/taos-ws-py-wrapper#1
Assignees
Labels
topic-multiprocessing type-bug An unexpected behavior, bug, or error

Comments

@z764969689
Copy link
Contributor

z764969689 commented May 16, 2023

Bug report

I have a project that uses threading.Thread and multiprocessing.Process for handling concurrent tasks. While under certain circumstances, there is the possibility that when I try new a Process() instance and call the Process.start() function, an error would occur:
AttributeError: 'NoneType' object has no attribute 'poll'

I tried debugging my project and found that under the multiprocessing.process module a global variable _children is used for managing all child processes, the variable is not thread-safe and the error would occur occasionally. I made a simple test code with minor modification on the multiprocessing.process

test.py

from multiprocessing import Process
from threading import Thread


def helper():
    time.sleep(0.1)
    return 1


def close_process(p_: Process):
    p.terminate()
    p_.close()


if __name__ == "__main__":
    process_list = []
    for _ in range(1):
        p = Process(target=helper)
        p.start()
        time.sleep(0.2)
        process_list.append(p)

    for _ in range(1):
        t = Thread(target=close_process, args=(process_list[_],))
        t.start()

    new_p = Process(target=helper)
    time.sleep(0.2)
    new_p.start()

multiprocessing.process.py class BaseProcess

    def close(self):
        '''
        Close the Process object.

        This method releases resources held by the Process object.  It is
        an error to call this method if the child process is still running.
        '''
        if self._popen is not None:
            if self._popen.poll() is None:
                raise ValueError("Cannot close a process while it is still running. "
                                 "You should first call join() or terminate().")
            self._popen.close()
            self._popen = None
            import time
            time.sleep(5)
            del self._sentinel
            _children.discard(self)
        self._closed = True

I simply add time.sleep() for BaseProcess.close() class and now every time I run the test.py, the error would occur.

Your environment

  • CPython versions tested on: python 3.7
  • Operating system and architecture: windows 10 or linux el7

Linked PRs

@z764969689 z764969689 added the type-bug An unexpected behavior, bug, or error label May 16, 2023
@gpshead gpshead self-assigned this May 20, 2023
gpshead added a commit that referenced this issue May 22, 2023
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 <[email protected]>
Co-authored-by: Gregory P. Smith <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 22, 2023
…thonGH-104537)

Fix a race condition in the internal `multiprocessing.process` cleanup
logic that could manifest as an unintended `AttributeError` when calling
`BaseProcess.close()`.

---------

(cherry picked from commit ef5d00a)

Co-authored-by: Luccccifer <[email protected]>
Co-authored-by: Oleg Iarygin <[email protected]>
Co-authored-by: Gregory P. Smith <[email protected]>
ambv pushed a commit that referenced this issue May 22, 2023
…H-104537) (#104737)

Fix a race condition in the internal `multiprocessing.process` cleanup
logic that could manifest as an unintended `AttributeError` when calling
`BaseProcess.close()`.

(cherry picked from commit ef5d00a)

Co-authored-by: Luccccifer <[email protected]>
Co-authored-by: Oleg Iarygin <[email protected]>
Co-authored-by: Gregory P. Smith <[email protected]>
gtopper added a commit to gtopper/taos-ws-py-wrapper that referenced this issue Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-multiprocessing type-bug An unexpected behavior, bug, or error
Projects
None yet
3 participants