Skip to content

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

Closed
gtopper/taos-ws-py-wrapper
#1

Description

@z764969689

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

Activity

  1. self-assigned this
    on May 20, 2023
  2. added a commit that references this issue on May 22, 2023
  3. added a commit that references this issue on May 22, 2023
  4. added a commit that references this issue on May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions