Skip to content

Commit

Permalink
Some more additional changes to the ZMQHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
s0undt3ch committed Jun 20, 2021
1 parent 60ca365 commit 00bcb53
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog/74.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Some more additional changes to the ZMQHandler to make sure it's resources are cleaned when terminating
21 changes: 16 additions & 5 deletions src/saltfactories/utils/saltext/log_handlers/pytest_log_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def start(self):

self.pid = os.getpid()

def stop(self):
def stop(self, flush=True):
if self._exiting:
return

Expand All @@ -266,8 +266,12 @@ def stop(self):

try:
if self.pusher is not None and not self.pusher.closed:
# Give it 1.5 seconds to flush any messages in it's queue
self.pusher.close(1500)
if flush:
# Give it 1.5 seconds to flush any messages in it's queue
linger = 1500
else:
linger = 0
self.pusher.close(linger)
self.pusher = None
if self.context is not None and not self.context.closed:
self.context.term()
Expand All @@ -282,7 +286,13 @@ def stop(self):
sys.stderr.flush()
raise
finally:
self.context = self.pusher = self.pid = None
if self.pusher is not None and not self.pusher.closed:
self.pusher.close(0)
self.pusher = None
if self.context is not None and not self.context.closed:
self.context.term()
self.context = None
self.pid = None

def format(self, record):
msg = super(ZMQHandler, self).format(record)
Expand Down Expand Up @@ -348,6 +358,7 @@ def emit(self, record):
except (SystemExit, KeyboardInterrupt): # pragma: no cover pylint: disable=try-except-raise
# Catch and raise SystemExit and KeyboardInterrupt so that we can handle
# all other exception below
self.stop(flush=False)
raise
except Exception: # pragma: no cover pylint: disable=broad-except
self.handleError(record)
Expand All @@ -366,7 +377,7 @@ def close(self):
Tidy up any resources used by the handler.
"""
# The logging machinery has asked to stop this handler
self.stop()
self.stop(flush=False)
# self._exiting should already be True, nonetheless, we set it here
# too to ensure the handler doesn't get a chance to restart itself
self._exiting = True
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/utils/saltext/test_log_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,13 @@ def test_all_messages_received_multiprocessing(tempfiles, salt_factories, caplog
def log_from_child_process(idx, parent_pid, evt):
evt.set() # process started, ready to start another one
import os
import time
import logging
log = logging.getLogger("foo")
for idx in range(1, {calls} + 1):
log.debug("Foo(Child of pid %s):%s:%s", parent_pid, idx, os.getpid())
exit(0)
def log_from_process(pidx, evt):
evt.set() # process started, ready to start another one
import os
import logging
Expand All @@ -215,6 +213,8 @@ def log_from_process(pidx, evt):
cevt.clear()
time.sleep(0.25)
evt.set() # process started, ready to start another one
log = logging.getLogger("foo")
for idx in range(1, {calls} + 1):
log.debug("Foo:%s:%s", idx, os.getpid())
Expand Down

0 comments on commit 00bcb53

Please sign in to comment.