You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "FTS18 (via GitHub)" <gi...@apache.org> on 2026/05/01 20:23:30 UTC

[PR] Fix TypeError in PercentFormatRender when numeric callsite parameters… [airflow]

FTS18 opened a new pull request, #66230:
URL: https://github.com/apache/airflow/pull/66230

   This PR fixes a critical crash in the scheduler (and other components) where `PercentFormatRender` would raise a `TypeError` when a numeric log format specifier (like `%d`) was used for callsite parameters (`process`, `thread`, `lineno`) that were missing or unknown.
   
   ### Problem
   As reported in #66195, during startup, certain warnings (like insecure key length) are emitted before the full logging context or `CallsiteParameterAdder` has had a chance to populate the event dictionary. In these cases, our internal `_LazyLogRecordDict` would return `None` or the string `"(unknown)"`. If the configured `log_format` uses `%d` for these fields, Python's string interpolation fails:
   `TypeError: %d format: a real number is required, not str`
   
   ### Solution
   Updated `_LazyLogRecordDict.__getitem__` in `shared/logging/src/airflow_shared/logging/percent_formatter.py` to explicitly handle `lineno`, `thread`, and `process`. It now returns `0` if the value is missing or equal to the `"(unknown)"` placeholder string.
   
   ### Changes
   - **Fix**: Logic added to `percent_formatter.py` to provide safe numeric defaults.
   - **Test**: Added `shared/logging/tests/logging/test_issue_66195.py` as a regression test.
   - **Newsfragment**: Added bugfix newsfragment.
   
   closes: #66195 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TypeError in PercentFormatRender when numeric callsite parameters… [airflow]

Posted by "parkhojeong (via GitHub)" <gi...@apache.org>.
parkhojeong commented on code in PR #66230:
URL: https://github.com/apache/airflow/pull/66230#discussion_r3180889575


##########
shared/logging/tests/logging/test_percent_formatter.py:
##########
@@ -40,3 +40,40 @@ def test_lineno_is_none(self):
         )
 
         assert formatted == "test.py:0 our msg"
+
+    @mock.patch("airflow_shared.logging.percent_formatter.os.getpid")
+    @mock.patch("airflow_shared.logging.percent_formatter.threading.get_ident")
+    def test_process_thread_missing_returns_live_values(self, mock_get_ident, mock_getpid):
+        mock_getpid.return_value = 123
+        mock_get_ident.return_value = 456
+        fmt = "%(process)d %(thread)d %(lineno)d %(message)s"
+        renderer = PercentFormatRender(fmt)
+
+        event_dict = {"event": "test message"}
+        result = renderer(None, "info", event_dict)
+        assert result.startswith("123 456 0 test message")
+
+    @mock.patch("airflow_shared.logging.percent_formatter.os.getpid")
+    @mock.patch("airflow_shared.logging.percent_formatter.threading.get_ident")
+    def test_process_thread_unknown_string_returns_live_values(self, mock_get_ident, mock_getpid):
+        mock_getpid.return_value = 123
+        mock_get_ident.return_value = 456
+        fmt = "%(process)d %(thread)d %(lineno)d %(message)s"
+        renderer = PercentFormatRender(fmt)
+
+        event_dict = {
+            "event": "test message",
+            "process": "(unknown)",
+            "thread": "(unknown)",
+            "lineno": "(unknown)",

Review Comment:
   the test includes lineno but function name doesn't have it.
   is there any reason using only two keys?



##########
shared/logging/tests/logging/test_percent_formatter.py:
##########
@@ -40,3 +40,40 @@ def test_lineno_is_none(self):
         )
 
         assert formatted == "test.py:0 our msg"
+
+    @mock.patch("airflow_shared.logging.percent_formatter.os.getpid")
+    @mock.patch("airflow_shared.logging.percent_formatter.threading.get_ident")
+    def test_process_thread_missing_returns_live_values(self, mock_get_ident, mock_getpid):
+        mock_getpid.return_value = 123
+        mock_get_ident.return_value = 456
+        fmt = "%(process)d %(thread)d %(lineno)d %(message)s"
+        renderer = PercentFormatRender(fmt)
+
+        event_dict = {"event": "test message"}
+        result = renderer(None, "info", event_dict)
+        assert result.startswith("123 456 0 test message")
+
+    @mock.patch("airflow_shared.logging.percent_formatter.os.getpid")
+    @mock.patch("airflow_shared.logging.percent_formatter.threading.get_ident")
+    def test_process_thread_unknown_string_returns_live_values(self, mock_get_ident, mock_getpid):
+        mock_getpid.return_value = 123
+        mock_get_ident.return_value = 456
+        fmt = "%(process)d %(thread)d %(lineno)d %(message)s"
+        renderer = PercentFormatRender(fmt)
+
+        event_dict = {
+            "event": "test message",
+            "process": "(unknown)",
+            "thread": "(unknown)",
+            "lineno": "(unknown)",

Review Comment:
   the test includes lineno but function name doesn't have it.
   is there any reason using only two keys(process, thread)?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TypeError in PercentFormatRender when numeric callsite parameters… [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #66230:
URL: https://github.com/apache/airflow/pull/66230#discussion_r3175306796


##########
shared/logging/tests/logging/test_issue_66195.py:
##########
@@ -0,0 +1,39 @@
+from __future__ import annotations
+
+from airflow_shared.logging.percent_formatter import PercentFormatRender
+
+
+class TestIssue66195:

Review Comment:
   Test files (and classes) should be named after the behavior they verify, not the issue number. These two methods belong in the existing `test_percent_formatter.py::TestPercentFormatRender` -- that class already has `test_no_callsite` and `test_lineno_is_none` covering the same surface. Suggest deleting this file and adding two methods to the existing class with descriptive names, e.g. `test_process_thread_missing_returns_zero` and `test_process_thread_unknown_string_returns_zero`.
   



##########
shared/logging/tests/logging/test_issue_66195.py:
##########
@@ -0,0 +1,39 @@
+from __future__ import annotations

Review Comment:
   The Apache 2.0 license header is missing from this new file. The `update-license-header` prek hook will fail CI on this. Copy the standard 17-line header from `test_percent_formatter.py:1-17`.
   



##########
shared/logging/src/airflow_shared/logging/percent_formatter.py:
##########
@@ -53,8 +53,11 @@ def __getitem__(self, key):
         # If there is no callsite info (often for stdout/stderr), show the same sort of thing that stdlib
         # logging would
         # https://github.com/python/cpython/blob/d3c888b4ec15dbd7d6b6ef4f15b558af77c228af/Lib/logging/__init__.py#L1652C34-L1652C48
-        if key == "lineno":
-            return self.event.get("lineno") or 0
+        if key in ("lineno", "thread", "process"):
+            val = self.event.get(key)
+            if val is None or val == "(unknown)":
+                return 0
+            return val

Review Comment:
   This is a defensive renderer-level patch, but it loses real diagnostic data and may be papering over the actual bug.
   
   **1. Root cause is probably upstream.** Look at `shared/logging/src/airflow_shared/logging/structlog.py:716` -- warning emission already routes through `CallsiteParameterAdder`. The fact that `process`/`thread` arrive here as `"(unknown)"` (rather than real PID/TID) suggests either `additional_ignores=[__name__]` is skipping the wrong frame, or `reconfigure_logger` isn't keeping the adder in the chain for the `py.warnings` logger. A fix at that layer would benefit every renderer, not just `PercentFormatRender`, and would preserve real values in JSON output too.
   
   **2. If we keep a renderer fallback, don't return `0`.** PID and thread ID are always obtainable from the current process via `os.getpid()` / `threading.get_ident()`. Returning `0` means every warning-derived log line will render `pid=0, thread=0` and obscure which scheduler/worker process actually emitted it -- which defeats the purpose of including those fields in the log format. Prefer something like:
   
   ```python
   import os, threading
   ...
   if key == "process":
       val = self.event.get("process")
       return val if val not in (None, "(unknown)") else os.getpid()
   if key == "thread":
       val = self.event.get("thread")
       return val if val not in (None, "(unknown)") else threading.get_ident()
   if key == "lineno":
       val = self.event.get("lineno")
       return val if val not in (None, "(unknown)") else 0
   ```
   
   **3. Minor semantic shift on `lineno`.** Old code was `self.event.get("lineno") or 0` (any falsy value collapsed to 0); the new code only collapses `None`/`"(unknown)"`. Probably fine, but worth noting in case anything downstream sets `lineno=""` or `lineno=False`.
   



##########
airflow-core/newsfragments/66230.bugfix.rst:
##########


Review Comment:
   This file was committed with a UTF-8 BOM (`EF BB BF`) and CRLF line endings, which is why GitHub renders it as a binary diff (`additions: 0, deletions: 0`, no patch). The towncrier release-notes builder won't pick it up correctly.
   
   Re-create as plain UTF-8 / LF, e.g.:
   
   ```bash
   rm airflow-core/newsfragments/66230.bugfix.rst
   printf 'Fix scheduler crash when ``InsecureKeyLengthWarning`` is emitted under a log format that uses ``%%(process)d`` or ``%%(thread)d``.\n' > airflow-core/newsfragments/66230.bugfix.rst
   ```
   
   Also worth tightening the wording -- right now it just restates the PR title. A release-notes reader cares about the user-visible symptom: "scheduler crashes at startup when the configured JWT secret is short enough to trigger `InsecureKeyLengthWarning`."
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TypeError in PercentFormatRender when numeric callsite parameters… [airflow]

Posted by "FTS18 (via GitHub)" <gi...@apache.org>.
FTS18 commented on PR #66230:
URL: https://github.com/apache/airflow/pull/66230#issuecomment-4363766184

   I have updated the PR to address all feedback from @kaxil:
   
   *   **Root Cause Fix**: Updated `_showwarning` in `structlog.py` to explicitly pass `process` and `thread` information during warning redirection. This ensures real diagnostic data is preserved for all renderers (including JSON).
   *   **Live Fallbacks**: Updated `PercentFormatRender` to use `os.getpid()` and `threading.get_ident()` as fallbacks for missing or unknown callsite parameters instead of returning `0`.
   *   **Test Consolidation**: Moved and improved the regression tests into `test_percent_formatter.py` with descriptive names and deterministic mocks. Deleted the standalone test file.
   *   **Newsfragment**: Recreated the newsfragment with plain UTF-8 encoding (no BOM) and LF line endings. Updated the wording to focus on the user-visible symptom.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TypeError in PercentFormatRender when numeric callsite parameters… [airflow]

Posted by "parkhojeong (via GitHub)" <gi...@apache.org>.
parkhojeong commented on code in PR #66230:
URL: https://github.com/apache/airflow/pull/66230#discussion_r3180889575


##########
shared/logging/tests/logging/test_percent_formatter.py:
##########
@@ -40,3 +40,40 @@ def test_lineno_is_none(self):
         )
 
         assert formatted == "test.py:0 our msg"
+
+    @mock.patch("airflow_shared.logging.percent_formatter.os.getpid")
+    @mock.patch("airflow_shared.logging.percent_formatter.threading.get_ident")
+    def test_process_thread_missing_returns_live_values(self, mock_get_ident, mock_getpid):
+        mock_getpid.return_value = 123
+        mock_get_ident.return_value = 456
+        fmt = "%(process)d %(thread)d %(lineno)d %(message)s"
+        renderer = PercentFormatRender(fmt)
+
+        event_dict = {"event": "test message"}
+        result = renderer(None, "info", event_dict)
+        assert result.startswith("123 456 0 test message")
+
+    @mock.patch("airflow_shared.logging.percent_formatter.os.getpid")
+    @mock.patch("airflow_shared.logging.percent_formatter.threading.get_ident")
+    def test_process_thread_unknown_string_returns_live_values(self, mock_get_ident, mock_getpid):
+        mock_getpid.return_value = 123
+        mock_get_ident.return_value = 456
+        fmt = "%(process)d %(thread)d %(lineno)d %(message)s"
+        renderer = PercentFormatRender(fmt)
+
+        event_dict = {
+            "event": "test message",
+            "process": "(unknown)",
+            "thread": "(unknown)",
+            "lineno": "(unknown)",

Review Comment:
   the test includes lineno but function name has process and thread.
   is there any reason using only two keys?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TypeError in PercentFormatRender when numeric callsite parameters… [airflow]

Posted by "parkhojeong (via GitHub)" <gi...@apache.org>.
parkhojeong commented on code in PR #66230:
URL: https://github.com/apache/airflow/pull/66230#discussion_r3180889575


##########
shared/logging/tests/logging/test_percent_formatter.py:
##########
@@ -40,3 +40,40 @@ def test_lineno_is_none(self):
         )
 
         assert formatted == "test.py:0 our msg"
+
+    @mock.patch("airflow_shared.logging.percent_formatter.os.getpid")
+    @mock.patch("airflow_shared.logging.percent_formatter.threading.get_ident")
+    def test_process_thread_missing_returns_live_values(self, mock_get_ident, mock_getpid):
+        mock_getpid.return_value = 123
+        mock_get_ident.return_value = 456
+        fmt = "%(process)d %(thread)d %(lineno)d %(message)s"
+        renderer = PercentFormatRender(fmt)
+
+        event_dict = {"event": "test message"}
+        result = renderer(None, "info", event_dict)
+        assert result.startswith("123 456 0 test message")
+
+    @mock.patch("airflow_shared.logging.percent_formatter.os.getpid")
+    @mock.patch("airflow_shared.logging.percent_formatter.threading.get_ident")
+    def test_process_thread_unknown_string_returns_live_values(self, mock_get_ident, mock_getpid):
+        mock_getpid.return_value = 123
+        mock_get_ident.return_value = 456
+        fmt = "%(process)d %(thread)d %(lineno)d %(message)s"
+        renderer = PercentFormatRender(fmt)
+
+        event_dict = {
+            "event": "test message",
+            "process": "(unknown)",
+            "thread": "(unknown)",
+            "lineno": "(unknown)",

Review Comment:
   the test includes lineno but function name has only process and thread.
   is there any reason using only two keys?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TypeError in PercentFormatRender when numeric callsite parameters… [airflow]

Posted by "parkhojeong (via GitHub)" <gi...@apache.org>.
parkhojeong commented on code in PR #66230:
URL: https://github.com/apache/airflow/pull/66230#discussion_r3180889575


##########
shared/logging/tests/logging/test_percent_formatter.py:
##########
@@ -40,3 +40,40 @@ def test_lineno_is_none(self):
         )
 
         assert formatted == "test.py:0 our msg"
+
+    @mock.patch("airflow_shared.logging.percent_formatter.os.getpid")
+    @mock.patch("airflow_shared.logging.percent_formatter.threading.get_ident")
+    def test_process_thread_missing_returns_live_values(self, mock_get_ident, mock_getpid):
+        mock_getpid.return_value = 123
+        mock_get_ident.return_value = 456
+        fmt = "%(process)d %(thread)d %(lineno)d %(message)s"
+        renderer = PercentFormatRender(fmt)
+
+        event_dict = {"event": "test message"}
+        result = renderer(None, "info", event_dict)
+        assert result.startswith("123 456 0 test message")
+
+    @mock.patch("airflow_shared.logging.percent_formatter.os.getpid")
+    @mock.patch("airflow_shared.logging.percent_formatter.threading.get_ident")
+    def test_process_thread_unknown_string_returns_live_values(self, mock_get_ident, mock_getpid):
+        mock_getpid.return_value = 123
+        mock_get_ident.return_value = 456
+        fmt = "%(process)d %(thread)d %(lineno)d %(message)s"
+        renderer = PercentFormatRender(fmt)
+
+        event_dict = {
+            "event": "test message",
+            "process": "(unknown)",
+            "thread": "(unknown)",
+            "lineno": "(unknown)",

Review Comment:
   lgtm overall !
   
   the test includes lineno but function name doesn't have it.
   is there any reason using only two keys(process, thread)?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TypeError in PercentFormatRender when numeric callsite parameters… [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #66230:
URL: https://github.com/apache/airflow/pull/66230#discussion_r3412757813


##########
shared/logging/tests/logging/test_percent_formatter.py:
##########
@@ -40,3 +40,40 @@ def test_lineno_is_none(self):
         )
 
         assert formatted == "test.py:0 our msg"
+
+    @mock.patch("airflow_shared.logging.percent_formatter.os.getpid")
+    @mock.patch("airflow_shared.logging.percent_formatter.threading.get_ident")
+    def test_process_thread_missing_returns_live_values(self, mock_get_ident, mock_getpid):
+        mock_getpid.return_value = 123
+        mock_get_ident.return_value = 456
+        fmt = "%(process)d %(thread)d %(lineno)d %(message)s"
+        renderer = PercentFormatRender(fmt)
+
+        event_dict = {"event": "test message"}
+        result = renderer(None, "info", event_dict)
+        assert result.startswith("123 456 0 test message")
+
+    @mock.patch("airflow_shared.logging.percent_formatter.os.getpid")
+    @mock.patch("airflow_shared.logging.percent_formatter.threading.get_ident")
+    def test_process_thread_unknown_string_returns_live_values(self, mock_get_ident, mock_getpid):

Review Comment:
   ```suggestion
       def test_unknown_string_returns_live_values(self, mock_get_ident, mock_getpid):
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TypeError in PercentFormatRender when numeric callsite parameters… [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #66230:
URL: https://github.com/apache/airflow/pull/66230#discussion_r3412760727


##########
shared/logging/tests/logging/test_percent_formatter.py:
##########
@@ -40,3 +40,40 @@ def test_lineno_is_none(self):
         )
 
         assert formatted == "test.py:0 our msg"
+
+    @mock.patch("airflow_shared.logging.percent_formatter.os.getpid")
+    @mock.patch("airflow_shared.logging.percent_formatter.threading.get_ident")
+    def test_process_thread_missing_returns_live_values(self, mock_get_ident, mock_getpid):

Review Comment:
   ```suggestion
       def test_missing_returns_live_values(self, mock_get_ident, mock_getpid):
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org