Skip to content

Conversation

@msom
Copy link
Contributor

@msom msom commented Sep 2, 2025

Currently, using the same ThreadMappingContext object in different threads throws a AttributeError: '_thread._local' object has no attribute 'data':

  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 376, in format
    message = self.formatMessage(record)
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 356, in formatMessage
    self._add_object_to_message(record, self.json_fmt, message)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 297, in _add_object_to_message
    message[key] = self._get_string_key_value(record, value)
                   ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 343, in _get_string_key_value
    return style.format(record)
           ~~~~~~~~~~~~^^^^^^^^
  File "/usr/lib/python3.13/logging/__init__.py", line 472, in format
    return self._format(record)
           ~~~~~~~~~~~~^^^^^^^^
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 114, in _format
    dct = {**record.__dict__, **flatten_dict(record.__dict__)}
                                ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 44, in flatten_dict
    return dict(_flatten_dict_gen(dct, parent_key, sep))
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 376, in format
    message = self.formatMessage(record)
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 356, in formatMessage
    self._add_object_to_message(record, self.json_fmt, message)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 297, in _add_object_to_message
    message[key] = self._get_string_key_value(record, value)
                   ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 343, in _get_string_key_value
    return style.format(record)
           ~~~~~~~~~~~~^^^^^^^^
  File "/usr/lib/python3.13/logging/__init__.py", line 472, in format
    return self._format(record)
           ~~~~~~~~~~~~^^^^^^^^
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 114, in _format
    dct = {**record.__dict__, **flatten_dict(record.__dict__)}
                                ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 44, in flatten_dict
    return dict(_flatten_dict_gen(dct, parent_key, sep))
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 38, in _flatten_dict_gen
    yield from flatten_dict(value, new_key, sep=sep).items()
               ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 44, in flatten_dict
    return dict(_flatten_dict_gen(dct, parent_key, sep))
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 35, in _flatten_dict_gen
    for key, value in dct.items():
                      ~~~~~~~~~^^
  File "<frozen _collections_abc>", line 898, in __iter__
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/context/thread_context.py", line 35, in __iter__
    return self.__local.data.__iter__()
           ^^^^^^^^^^^^^^^^^
AttributeError: '_thread._local' object has no attribute 'data'
", line 38, in _flatten_dict_gen
    yield from flatten_dict(value, new_key, sep=sep).items()
               ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 44, in flatten_dict
    return dict(_flatten_dict_gen(dct, parent_key, sep))
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/formatters/json_formatter.py", line 35, in _flatten_dict_gen
    for key, value in dct.items():
                      ~~~~~~~~~^^
  File "<frozen _collections_abc>", line 898, in __iter__
  File "/home/ltsom/Documents/repos/mf-chsdi3/.venv/lib/python3.13/site-packages/logging_utilities/context/thread_context.py", line 35, in __iter__
    return self.__local.data.__iter__()
           ^^^^^^^^^^^^^^^^^
AttributeError: '_thread._local' object has no attribute 'data'

Seems like the constructor is only executed in the thread the object is actually created but not in the other ones - not sure if this was different in Python versions <3.9. I've added a small helper that ensures that the self.__local objects have a data attribute before reading / writing. I've also added a test that actually tests if the data is thread-local (and that now passes without AttributeError).

@msom msom requested a review from ltshb September 2, 2025 10:12
@github-actions github-actions bot added the bug label Sep 2, 2025
@github-actions github-actions bot changed the title Fix AttributeError: '_thread._local' object has no attribute 'data' Fix AttributeError: '_thread._local' object has no attribute 'data' - #patch Sep 2, 2025
@msom msom changed the title Fix AttributeError: '_thread._local' object has no attribute 'data' - #patch PB-1920: Fix AttributeError: '_thread._local' object has no attribute 'data' - #patch Sep 2, 2025
@msom msom force-pushed the fix-thread-local branch 2 times, most recently from 4257c9b to e6e9001 Compare September 2, 2025 11:12
@msom msom merged commit 5033c55 into master Sep 3, 2025
3 checks passed
@msom msom deleted the fix-thread-local branch September 3, 2025 05:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants