]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-143237: Fix support of named pipes in the rotating logging handlers (GH-143259)
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 30 Dec 2025 14:56:29 +0000 (16:56 +0200)
committerGitHub <noreply@github.com>
Tue, 30 Dec 2025 14:56:29 +0000 (14:56 +0000)
This fixes regression introduced in GH-105887.

Lib/logging/handlers.py
Lib/test/test_logging.py
Misc/NEWS.d/next/Library/2025-12-28-20-28-05.gh-issue-143237.q1ymuA.rst [new file with mode: 0644]

index 2748b5941eade2ee56c5d1ade261702271af8704..4a07258f8d6d07ef487d0f3d2773109be8bf720d 100644 (file)
@@ -196,7 +196,11 @@ class RotatingFileHandler(BaseRotatingHandler):
         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
index 8815426fc99c39613845c07f373658c557a56e8b..848084e6e368784f8c01d070ed60d131d26aeee5 100644 (file)
@@ -25,6 +25,7 @@ import logging.config
 
 import codecs
 import configparser
+import contextlib
 import copy
 import datetime
 import pathlib
@@ -6369,6 +6370,32 @@ class RotatingFileHandlerTest(BaseFileTest):
         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)
+
+        data = 'not read'
+        def other_side():
+            nonlocal data
+            with open(filename, 'rb') as f:
+                data = f.read()
+
+        thread = threading.Thread(target=other_side)
+        with threading_helper.start_threads([thread]):
+            rh = logging.handlers.RotatingFileHandler(
+                    filename, encoding="utf-8", maxBytes=1)
+            with contextlib.closing(rh):
+                m = self.next_rec()
+                self.assertFalse(rh.shouldRollover(m))
+                rh.emit(m)
+        self.assertEqual(data.decode(), m.msg + os.linesep)
+
     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 (file)
index 0000000..131bebc
--- /dev/null
@@ -0,0 +1 @@
+Fix support of named pipes in the rotating :mod:`logging` handlers.