Skip to content

Comments

Restore _write and _flush in WriteLogger.__setstate__#787

Open
bysiber wants to merge 1 commit intohynek:mainfrom
bysiber:fix/writelogger-setstate-attrs
Open

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

Conversation

@bysiber
Copy link

@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

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

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