fix: prevent race condition in step content persistence#2796
Open
hztBUAA wants to merge 1 commit intoChainlit:mainfrom
Open
fix: prevent race condition in step content persistence#2796hztBUAA wants to merge 1 commit intoChainlit:mainfrom
hztBUAA wants to merge 1 commit intoChainlit:mainfrom
Conversation
Convert empty string input/output to NULL before persisting to database, so that the existing COALESCE logic in the upsert query correctly preserves content when the initial send() (with empty output) commits after update() (with real output). Fixes Chainlit#2789
dokterbob
reviewed
Feb 23, 2026
| "thread_id": step_dict.get("threadId"), | ||
| "parent_id": step_dict.get("parentId"), | ||
| "input": step_dict.get("input"), | ||
| "input": step_dict.get("input") or None, |
Collaborator
There was a problem hiding this comment.
Seems this would be a more elegant approach.
Suggested change
| "input": step_dict.get("input") or None, | |
| "input": step_dict.get("input", None) |
Collaborator
|
@hztBUAA Would be GREAT to have a regression (unit) test demonstrating the issue on main (e.g. the test fails), then passing on your PR! |
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.
Summary
Fixes #2789
Race condition in
ChainlitDataLayercauses Steps to lose content when the initialstep.send()(with empty output) finishes after the finalstep.update()(with real output). TheCOALESCEin the upsert SQL treats empty string as a valid value, so it overwrites the actual content instead of preserving it.Root cause
cl.Stepinitializesinputandoutputas empty strings ("")create_stepSQL usesCOALESCE(EXCLUDED.output, "Step".output)to preserve existing valuesCOALESCEonly skipsNULL, not empty strings — so an empty string from the initialsend()overwrites real content written byupdate()Fix
Convert empty string
input/outputtoNULLbefore passing to the database query. This allows the existingCOALESCElogic to correctly preserve content when a late-arrivingsend()would otherwise overwrite it.Test plan
or Noneunchanged)Summary by cubic
Prevents step content loss when send() (empty output) finishes after update() (real output). Empty input/output are now stored as NULL so the upsert COALESCE preserves existing content.
Written for commit 4544aa8. Summary will update on new commits.