Skip to content

fix(memory): make SQLAlchemySession first writes race-safe#2725

Merged
seratch merged 7 commits intoopenai:mainfrom
FelmonFekadu:fix/sqlalchemy-session-first-access
Mar 20, 2026
Merged

fix(memory): make SQLAlchemySession first writes race-safe#2725
seratch merged 7 commits intoopenai:mainfrom
FelmonFekadu:fix/sqlalchemy-session-first-access

Conversation

@FelmonFekadu
Copy link
Contributor

Summary

  • serialize one-time table creation for SQLAlchemySession so concurrent first access cannot race create_tables=True
  • guard first-write session row creation with the existing session lock and a conflict-safe insert path
  • add regression tests for concurrent first access both before and after schema initialization

Testing

Fixes #2722

@github-actions github-actions bot added bug Something isn't working feature:sessions labels Mar 19, 2026
@seratch seratch added this to the 0.12.x milestone Mar 19, 2026
@seratch seratch enabled auto-merge (squash) March 19, 2026 23:33
auto-merge was automatically disabled March 19, 2026 23:41

Head branch was pushed to by a user without write access

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 06b4e9c6f2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b6928587b0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 151382e82a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 61b7ace6eb

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a12dab769d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +58 to +59
_table_init_locks: ClassVar[dict[tuple[str, str, str], threading.Lock]] = {}
_table_init_locks_guard: ClassVar[threading.Lock] = threading.Lock()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Guard first-time table creation across worker processes

This lock table only exists inside one Python process. In a common deployment where several workers/processes call SQLAlchemySession.from_url(..., create_tables=True) against the same empty database, each process will still reach metadata.create_all() independently, so the original table already exists/dropped first write race can still happen on first access. The new tests only exercise threads/event loops within a single process, so this fix remains incomplete for multi-worker servers.

Useful? React with 👍 / 👎.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codex For production deployments, create_tables=True is not recommended and this option is useful mainly for local development or demo purposes. So, I think this is fine for now. Can you review again to see if there are any other things to mention?

@FelmonFekadu
Copy link
Contributor Author

FelmonFekadu commented Mar 20, 2026

@seratch latest push is green now.

I addressed the issues from the earlier review threads and replied inline with the verification details. Codex did leave one more comment about cross-process create_tables=True coordination when multiple workers hit the same empty database. That feels like a broader multiprocess deployment concern than the single-process race this PR started from, so I did not keep widening the patch on my own.

If you want this PR to cover that too, I can take a look at a DB-native approach. Otherwise I think the current branch is in a good place for your review.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a12dab769d

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@seratch seratch merged commit aa483fe into openai:main Mar 20, 2026
9 checks passed
@seratch seratch modified the milestones: 0.12.x, 0.13.x Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working feature:sessions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQLAlchemySession can fail and drop writes on concurrent first access

2 participants