Skip to content

Add _finish_when_safe to work around DBD::Pg race condition#89

Merged
kraih merged 1 commit intomojolicious:mainfrom
TFBW:main
Sep 29, 2025
Merged

Add _finish_when_safe to work around DBD::Pg race condition#89
kraih merged 1 commit intomojolicious:mainfrom
TFBW:main

Conversation

@TFBW
Copy link
Contributor

@TFBW TFBW commented Sep 26, 2025

Summary

Adds a private method _finish_when_safe in Mojo::Pg::Database, used by Mojo::Pg::Results instead of calling finish directly on DBI statement handles.

Motivation

There is a race condition in DBD::Pg which causes in-progress statement handles to be finished inappropriately when old statement handles are finished during an async DB query. This issue was previously concealed by a memory leak in Mojo::Promise which kept affected objects alive more or less indefinitely. Now that the leak is fixed, this DBD::Pg bug can cause async DB operations in close proximity to fail.

This change works around the problem by postponing finish operations if an async query is in progress. Most of the time there will be no such query in progress, and the handle is finished immediately. Where unsafe, a temporary array is created to hold any incoming statement handles, and the contents of this array (if any) are finished when the async operation completes.

References

@kraih
Copy link
Member

kraih commented Sep 27, 2025

I've fixed the tests, please rebase on main.

@kraih kraih merged commit f97855a into mojolicious:main Sep 29, 2025
7 checks passed
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