Skip to content

logging.StreamHandler performs two syscalls when one would do #79227

Closed

Description

@hashbrowncipher
BPO 35046
Nosy @vsajip, @tirkarthi, @hashbrowncipher
PRs
  • bpo-35046: do only one system call per line (logging.StreamHandler) #10042
  • bpo-35046: coalesced two syscalls into one in logging. #10043
  • [3.7] bpo-35046: do only one system call per line (logging.StreamHandler) (GH-10042) #10050
  • 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 2018-10-23.10:07:47.482>
    created_at = <Date 2018-10-22.18:51:21.803>
    labels = ['type-bug', '3.7']
    title = 'logging.StreamHandler performs two syscalls when one would do'
    updated_at = <Date 2019-02-06.14:24:59.625>
    user = 'https://github.com/hashbrowncipher'

    bugs.python.org fields:

    activity = <Date 2019-02-06.14:24:59.625>
    actor = 'ulope'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-10-23.10:07:47.482>
    closer = 'vinay.sajip'
    components = []
    creation = <Date 2018-10-22.18:51:21.803>
    creator = 'josnyder'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 35046
    keywords = ['patch']
    message_count = 4.0
    messages = ['328269', '328286', '328295', '334938']
    nosy_count = 4.0
    nosy_names = ['vinay.sajip', 'ulope', 'xtreak', 'josnyder']
    pr_nums = ['10042', '10043', '10050']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue35046'
    versions = ['Python 3.7']

    Activity

    1. hashbrowncipher commented on Oct 22, 2018

      hashbrownciphermannequin
      MannequinAuthor

      logging.StreamHandler contains the following code:

          stream.write(msg)
          stream.write(self.terminator)
          stream.flush()

      When sys.stderr (or whatever other stream) is unbuffered, this results in two system calls and allows log records from different processes to concatenate on the same line in the output stream (followed by multiple newlines). This issue is new in Python 3.7, as stdout and stderr became "truly unbuffered" (cf. bpo-30404).

      As a simple solution, I believe the following would fix the issue and also be backward compatible:

          stream.write(msg + self.terminator)
          stream.flush()
    2. vsajip commented on Oct 23, 2018

      @vsajip
      Member

      New changeset b7d6205 by Vinay Sajip (Josh Snyder) in branch 'master':
      bpo-35046: do only one system call per line (logging.StreamHandler) (GH-10042)
      b7d6205

    3. vsajip commented on Oct 23, 2018

      @vsajip
      Member

      New changeset d730719 by Vinay Sajip (Miss Islington (bot)) in branch '3.7':
      bpo-35046: do only one system call per line (logging.StreamHandler) (GH-10042) (GH-10050)
      d730719

    4. ulope commented on Feb 6, 2019

      ulopemannequin
      Mannequin

      This change breaks (probably unsupported) uses of the logging module (esp. in combination with structlog) where non-string objects are passed through the .format() and .emit() calls.

      Previously it was possible to set the terminator attribute to None (and then handling that case in a custom 'stream' implementation), which now breaks with a TypeError due to the concatenation.

      In case anyone else runs into this (however unlikely that is). A workaround is to set the .terminator to something like this:

          class ConcatenableNothing:
              def __radd__(self, other):
                  return other
    5. transferred this issue fromon Apr 10, 2022
    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 lifetype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions