8000 bpo-6721: Sanitize logging locks while forking by gpshead · Pull Request #4071 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-6721: Sanitize logging locks while forking #4071

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

Merged
merged 7 commits into from
Sep 14, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a unittest!
  • Loading branch information
gpshead committed Sep 13, 2018
commit ebb4b743826263f3400c66fa2c55fdef5968264d
39 changes: 39 additions & 0 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import queue
import random
import re
import signal
import socket
import struct
import sys
Expand Down Expand Up @@ -666,6 +667,44 @@ def remove_loop(fname, tries):
if os.path.exists(fn):
os.unlink(fn)

# The implementation relies on os.register_at_fork existing, but we test
# based on os.fork existing because that is what users and this test use.
# This helps ensure that when fork exists (the important concept) that the
# register_at_fork mechanism is also present and used.
@unittest.skipIf(not hasattr(os, 'fork'), 'Test requires os.fork().')
def test_post_fork_child_no_deadlock(self):
"""Ensure forked child logging locks are not held; bpo-6721."""
refed_h = logging.Handler()
refed_h.name = 'because we need at least one for this test'
logging._acquireLock()
try:
self.assertGreater(len(logging._handlers), 0)
the_handler = next(iter(logging._handlers.values()))
the_handler.acquire()
try:
pid = os.fork()
if pid == 0: # Child.
logging.error(r'Child process did not deadlock. \o/')
os._exit(0)
else: # Parent.
start_time = time.monotonic()
while True:
waited_pid, status = os.waitpid(pid, os.WNOHANG)
if waited_pid == pid:
break # child process exited.
if time.monotonic() - start_time > 20:
break # so long? implies child deadlock.
time.sleep(0.05)
if waited_pid != pid:
os.kill(pid, signal.SIGKILL)
waited_pid, status = os.waitpid(pid, 0)
self.fail("child process deadlocked.")
self.assertEqual(status, 0, msg="child process error")
finally:
the_handler.release()
finally:
logging._releaseLock()


class BadStream(object):
def write(self, data):
Expand Down
0