Skip to content

multiprocessing.Process depends on sys.stdout being open #72513

Closed

Description

@progval
mannequin
BPO 28326
Nosy @terryjreedy, @pitrou, @tiagoantao, @progval, @applio, @Mariatta, @glolol
PRs
  • bpo-28326: Fixes multiprocessing.Process depends on sys.stdout being open #1410
  • bpo-28326: Fix multiprocessing.Process when stdout and/or stderr is closed or None. #4073
  • [3.6] bpo-28326: Fix multiprocessing.Process when stdout and/or stderr is closed or None. (GH-4073).  #4075
  • Files
  • mp.patch: Patch with test case
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-10-22.10:27:53.597>
    created_at = <Date 2016-10-01.11:45:34.763>
    labels = ['3.7', 'type-bug', 'library']
    title = 'multiprocessing.Process depends on sys.stdout being open'
    updated_at = <Date 2017-10-22.10:27:53.595>
    user = 'https://github.com/ProgVal'

    bugs.python.org fields:

    activity = <Date 2017-10-22.10:27:53.595>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-10-22.10:27:53.597>
    closer = 'pitrou'
    components = ['Library (Lib)']
    creation = <Date 2016-10-01.11:45:34.763>
    creator = 'Valentin.Lorentz'
    dependencies = []
    files = ['44919']
    hgrepos = []
    issue_num = 28326
    keywords = ['patch']
    message_count = 8.0
    messages = ['277807', '277849', '278271', '278273', '304713', '304737', '304740', '304741']
    nosy_count = 7.0
    nosy_names = ['terry.reedy', 'pitrou', 'tiagoantao', 'Valentin.Lorentz', 'davin', 'Mariatta', 'tacocat']
    pr_nums = ['1410', '4073', '4075']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue28326'
    versions = ['Python 3.6', 'Python 3.7']

    Activity

    1. progval commented on Oct 1, 2016

      progvalmannequin
      MannequinAuthor

      Hello,

      The following code:

      import sys
      import multiprocessing
      
      sys.stdout.close()
      
      def foo():
          pass
      
      p = multiprocessing.Process(target=foo)
      p.start()

      Crashes with:

      Traceback (most recent call last):
        File "foo.py", line 10, in <module>
          p.start()
        File "/usr/lib/python3.5/multiprocessing/process.py", line 105, in start
          self._popen = self._Popen(self)
        File "/usr/lib/python3.5/multiprocessing/context.py", line 212, in _Popen
          return _default_context.get_context().Process._Popen(process_obj)
        File "/usr/lib/python3.5/multiprocessing/context.py", line 267, in _Popen
          return Popen(process_obj)
        File "/usr/lib/python3.5/multiprocessing/popen_fork.py", line 17, in __init__
          sys.stdout.flush()
      ValueError: I/O operation on closed file.

      This bug has been reported to me on a daemonized program (written prior to PEP-3143).

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Oct 1, 2016
    3. tiagoantao commented on Oct 2, 2016

      tiagoantaomannequin
      Mannequin

      I made a small patch for this. This is the first time I submit one, so please be careful with this...

    4. terryjreedy commented on Oct 7, 2016

      @terryjreedy
      Member

      Patch looks plausible to me. The flush calls definitely need to be guarded.

    5. tiagoantao commented on Oct 7, 2016

      tiagoantaomannequin
      Mannequin

      This is the first patch that I am submitting. More than willing to do any changes deemed necessary...

    6. pitrou commented on Oct 21, 2017

      @pitrou
      Member

      In bpo-31804, we see that sys.stdout and sys.stderr can sometimes simply be None, so we must check for that too.

    7. pitrou commented on Oct 22, 2017

      @pitrou
      Member

      New changeset daeefd2 by Antoine Pitrou in branch 'master':
      bpo-28326: Fix multiprocessing.Process when stdout and/or stderr is closed or None. (bpo-4073)
      daeefd2

    8. pitrou commented on Oct 22, 2017

      @pitrou
      Member

      New changeset 34ef6da by Antoine Pitrou in branch '3.6':
      [3.6] bpo-28326: Fix multiprocessing.Process when stdout and/or stderr is closed or None. (GH-4073). (bpo-4075)
      34ef6da

    9. pitrou commented on Oct 22, 2017

      @pitrou
      Member

      This is now fixed in 3.6 and 3.7. Closing!

    10. transferred this issue fromon Apr 10, 2022
    11. betterlch commented on Sep 14, 2026

      @betterlch

      Hello , I met some error when using multiprocessing

      Process.start() raises BrokenPipeError: [Errno 32] Broken pipe when stdout is a pipe with a dead read end — _flush_std_streams() does not catch OSError

      def _flush_std_streams():
          try:
              sys.stdout.flush()
          except (AttributeError, ValueError):   # BrokenPipeError (an OSError) escapes
              pass
          ... 

      If sys.stdout holds unflushed buffered data and its pipe's read end is gone, sys.stdout.flush() raises BrokenPipeError, which propagates out of Process.start().

      import os, sys, multiprocessing
      
      def target():
          pass
      
      if __name__ == "__main__":
          # Simulate a scheduler-launched process whose stdout reader exited.
          r, w = os.pipe()
          os.dup2(w, 1)                    # fd 1 -> the pipe
          os.close(r)                      # read end gone -> broken pipe
          os.close(w)                      # fd 1 keeps the write end alive
          sys.stdout = os.fdopen(1, "w")   # block-buffered (pipe, not a tty)
      
          sys.stdout.write("unflushed data")   # stays in the userspace buffer
      
          p = multiprocessing.Process(target=target)
          p.start()   # raises BrokenPipeError
          p.join()

      How we use: the process was launched over SSH (paramiko exec_command), and the caller closed the channel without draining output

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions