Ensure logging is configured on remote task workers#21379
Merged
desertaxle merged 4 commits intomainfrom Apr 1, 2026
Merged
Conversation
When tasks execute on remote Dask/Ray workers via a serialized context, the worker process may never have called setup_logging(), so the APILogHandler is missing from Prefect loggers and task logs are silently lost. Add ensure_logging_setup() API in logging/configuration.py that encapsulates the PROCESS_LOGGING_CONFIG check and only calls setup_logging() if needed. Call it from both SyncTaskRunEngine and AsyncTaskRunEngine initialize_run() when a serialized context is present. Flush APILogHandler in the finally block to ensure logs are sent before the task result is returned. Closes #18082 Co-authored-by: Alex Streed <desertaxle@users.noreply.github.com> Co-Authored-By: alex.s <ajstreed1@gmail.com>
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
The flush was running on all task runs (including local ThreadPool runs
where self.context is set to {cancel_event: ...}), disrupting the
EventsWorker service. The core fix (ensure_logging_setup) is sufficient
for the remote worker logging issue.
Co-authored-by: Alexander Streed <desertaxle@users.noreply.github.com>
Co-Authored-By: alex.s <ajstreed1@gmail.com>
desertaxle
requested changes
Apr 1, 2026
…ve is_logging_configured - Moved ensure_logging_setup() call from task_engine.py into hydrated_context() in context.py, so it runs automatically when a serialized context is provided (remote execution) - Removed is_logging_configured() since it was only used in tests - Removed ensure_logging_setup import/calls from task_engine.py Co-authored-by: Alexander Streed <desertaxle@users.noreply.github.com> Co-Authored-By: alex.s <ajstreed1@gmail.com>
desertaxle
requested changes
Apr 1, 2026
src/prefect/context.py
Outdated
|
|
||
| with ExitStack() as stack: | ||
| if serialized_context: | ||
| from prefect.logging.configuration import ensure_logging_setup |
Member
There was a problem hiding this comment.
Don't defer this import unless it's absolutely necessary.
Contributor
Author
There was a problem hiding this comment.
Moved to top-level import in f0eee82. Verified no circular import issue.
Co-authored-by: Alexander Streed <desertaxle@users.noreply.github.com> Co-Authored-By: alex.s <ajstreed1@gmail.com>
desertaxle
approved these changes
Apr 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When tasks run on remote Dask or Ray workers via a serialized context, the worker process may never have called
setup_logging(). This is becausesetup_logging()lives inprefect.main, which is lazily imported viaprefect.__getattr__— remote workers that don't access names likeprefect.floworprefect.tasknever trigger that import. As a result, theAPILogHandleris not attached to Prefect loggers and task run logs are silently lost.Closes #18082
Closes #10829
Changes
New API in
prefect.logging.configuration:ensure_logging_setup()— idempotent function that callssetup_logging()only ifPROCESS_LOGGING_CONFIGhas not been populated. This keeps the internal state check encapsulated rather than leaking it to callers.is_logging_configured()— simple query for whether logging has been set up in this process.Task engine (
SyncTaskRunEngine/AsyncTaskRunEngine):initialize_run(), after enteringhydrated_context, callensure_logging_setup()whenself.context is not None(remote execution signal). For local task runs,PROCESS_LOGGING_CONFIGis already populated via the normalprefect.mainimport, so the call is a no-op.Notes for reviewers
self.context is not Noneis used to gate the call, but local ThreadPool runs also setcontext(withcancel_event). This is fine becauseensure_logging_setup()short-circuits via thePROCESS_LOGGING_CONFIGcheck — zero overhead for local runs.is_logging_configured()is added and tested but not called by the implementation itself. It's exposed as public API for external consumers.dictConfigMockfixture pattern.Checklist
<link to issue>"mint.json.Link to Devin session: https://app.devin.ai/sessions/c7590302b0bc40df9fba83c32ba15f91
Requested by: @desertaxle