Skip to content

Fix Flow.chat() to restore defer_trace_finalization when finalization raises - #8637

Open
NSK-syam wants to merge 1 commit into
crewAIInc:mainfrom
NSK-syam:fix/flow-chat-restore-defer-trace-on-finalization-error
Open

NSK-syam wants to merge 1 commit into
crewAIInc:mainfrom
NSK-syam:fix/flow-chat-restore-defer-trace-on-finalization-error

Conversation

@NSK-syam

@NSK-syam NSK-syam commented Oct 26, 2026 •

Copy link
Copy Markdown

Summary

Fixes #8069.

Flow.chat() temporarily enables defer_trace_finalization and restores its previous value in a finally block. finalize_session_traces() was called before the restoration, so if it raised, a reused Flow instance retained the temporary setting.

Wrap finalization in a try/finally so the previous value is restored even when finalization raises, while still propagating the error.

Reproduction

Before fix: before: False, RuntimeError: finalization failed, after: True.
After fix: before: False, RuntimeError: finalization failed, after: False.

Tests

  • Ran test_flow_conversation.py (174 passed).
  • Verified the exact reproduction from the issue.

Scope

Only touches the existing chat() cleanup. No new session manager, retry loop, or tracing API; finalization errors are not suppressed.


Note

Low Risk
Single cleanup-path change in conversational chat() with no behavior change when finalization succeeds.

Overview
Fixes a cleanup bug in Flow.chat() when finalize_session_traces() fails at the end of the REPL loop.

chat() temporarily sets defer_trace_finalization and is supposed to restore the previous value in a finally block. Previously, restoration ran in the same finally after finalization, so if finalization raised, the flag stayed True on a reused flow instance.

The change nests try/finally so the prior defer_trace_finalization value is always restored; finalization errors still propagate unchanged.

Reviewed by Cursor Bugbot for commit d7d57c0. Bugbot is set up for automated code reviews on this repo. Configure here.

… raises

Flow.chat() temporarily enables defer_trace_finalization and restores its
previous value in a finally block. finalize_session_traces() was called
before the restoration, so if it raised, a reused Flow instance retained
the temporary setting.

Wrap finalization in a try/finally so the previous value is restored even
when finalization raises, while still propagating the error.

Fixes crewAIInc#8069
@strix-security

strix-security Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Strix Security Review

✅ No open security findings.

PR summary: Fixes `Flow.chat()` so that the temporary `defer_trace_finalization` flag is restored even when trace finalization raises, by wrapping `finalize_session_traces()` in an additional `try/finally`. The finalization error is still propagated.

Review summary

Review of the single changed hunk in lib/crewai/src/crewai/flow/conversational_mixin.py is complete. The change nests the restoration of self.defer_trace_finalization in an inner try/finally around finalize_session_traces(), so the temporary deferral flag is reset even when finalization raises while the exception still propagates unchanged. This only tightens cleanup of instance state on the error path: no input handling, authentication, authorization, query construction, file or network operations, secret handling, or trust boundaries are touched, and no security control is removed. The restored value comes from getattr(self, "defer_trace_finalization", False) and is a boolean field, so no new failure mode is introduced on the exception path. No security issues found in the changed code.

Review 1 on this PR · d7d57c0 · requested Oct 26 23:39 UTC when the PR was opened


Reviewed by Strix
Re-run review · Configure security review settings

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-26T23:40:27.903666Z
We couldn’t complete this review. Comment @codex review to try again.
d7d57c0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

This branch has not been deployed

No deployments
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.

[BUG] Restore Flow.chat() configuration when trace finalization fails

1 participant