From cc46702ef7b6a3ff339e253f3f1246095a139fbe Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 28 Dec 2025 20:28:17 +0200 Subject: [PATCH] gh-143237: Fix support of named pipes in the rotating logging handlers This fixes regression introduced in GH-105887. --- Lib/logging/handlers.py | 6 ++++- Lib/test/test_logging.py | 23 +++++++++++++++++++ ...-12-28-20-28-05.gh-issue-143237.q1ymuA.rst | 1 + 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-12-28-20-28-05.gh-issue-143237.q1ymuA.rst diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py index 2748b5941eade2..4a07258f8d6d07 100644 --- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -196,7 +196,11 @@ def shouldRollover(self, record): if self.stream is None: # delay was set... self.stream = self._open() if self.maxBytes > 0: # are we rolling over? - pos = self.stream.tell() + try: + pos = self.stream.tell() + except io.UnsupportedOperation: + # gh-143237: Never rollover a named pipe. + return False if not pos: # gh-116263: Never rollover an empty file return False diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 8815426fc99c39..cf712ef4aa1a53 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -6369,6 +6369,29 @@ def test_should_not_rollover_non_file(self): self.assertFalse(rh.shouldRollover(self.next_rec())) rh.close() + @unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()') + def test_should_not_rollover_named_pipe(self): + # gh-143237 - test with non-seekable special file (named pipe) + filename = os_helper.TESTFN + self.addCleanup(os_helper.unlink, filename) + try: + os.mkfifo(filename) + except PermissionError as e: + self.skipTest('os.mkfifo(): %s' % e) + + def other_side(): + with open(filename, 'rb') as f: + f.read(1) + + thread = threading.Thread(target=other_side) + with threading_helper.start_threads([thread]): + rh = logging.handlers.RotatingFileHandler( + filename, encoding="utf-8", maxBytes=1) + try: + self.assertFalse(rh.shouldRollover(self.next_rec())) + finally: + rh.close() + def test_should_rollover(self): with open(self.fn, 'wb') as f: f.write(b'\n') diff --git a/Misc/NEWS.d/next/Library/2025-12-28-20-28-05.gh-issue-143237.q1ymuA.rst b/Misc/NEWS.d/next/Library/2025-12-28-20-28-05.gh-issue-143237.q1ymuA.rst new file mode 100644 index 00000000000000..131bebcd984ac2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-12-28-20-28-05.gh-issue-143237.q1ymuA.rst @@ -0,0 +1 @@ +Fix support of named pipes in the rotating :mod:`logging` handlers.