Skip to content

Restore _write and _flush in WriteLogger.__setstate__#787

Closed
bysiber wants to merge 1 commit into
hynek:mainfrom
bysiber:fix/writelogger-setstate-attrs
Closed

Restore _write and _flush in WriteLogger.__setstate__#787
bysiber wants to merge 1 commit into
hynek:mainfrom
bysiber:fix/writelogger-setstate-attrs

Conversation

@bysiber
Copy link
Copy Markdown

@bysiber bysiber commented Feb 20, 2026

WriteLogger.__setstate__ restores _file and _lock but not _write and _flush. After unpickling, any log method raises AttributeError:

import pickle
from structlog import WriteLogger

logger = pickle.loads(pickle.dumps(WriteLogger()))
logger.info("hello")
# AttributeError: 'WriteLogger' object has no attribute '_write'

BytesLogger.__setstate__ already restores all four attributes correctly, and WriteLogger.__deepcopy__ does too — this was just missed in __setstate__.

The fix adds the two missing assignments after restoring _file.

After unpickling a WriteLogger, calling any log method (msg, info,
debug, etc.) raises AttributeError because __setstate__ only restores
_file and _lock but not _write and _flush.

BytesLogger.__setstate__ already handles this correctly, and so does
WriteLogger.__deepcopy__ — this was just an oversight in __setstate__.
@RonnyPfannschmidt
Copy link
Copy Markdown

@hynek heads up , this one has been running a AI DOS on pytest and pluggy reviewer capacity

@hynek hynek closed this in #811 May 10, 2026
hynek added a commit that referenced this pull request May 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants