Skip to content
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

Some more additional changes to the ZMQHandler #74

Merged
merged 1 commit into from
Jun 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
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