Skip to content

Fix error handling for exceptions on values parsing.#3574

Open
APshenkin wants to merge 2 commits intobrianc:masterfrom
APshenkin:fix/fix-params-parsing-exceptions
Open

Fix error handling for exceptions on values parsing.#3574
APshenkin wants to merge 2 commits intobrianc:masterfrom
APshenkin:fix/fix-params-parsing-exceptions

Conversation

@APshenkin
Copy link

Fixes #3573

  • Fix: Avoid connection leaks by ensuring parse closure on error.
  • Refactor: Move Writer instantiation to improve isolation and prevent state reuse issues.

Copy link
Collaborator

@charmander charmander left a comment

Choose a reason for hiding this comment

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

Please add tests if possible. (Ideally, point out the minimal fix for each bug and which changes are just cleanup, e.g. by separating into 2–3 commits, but the tests will help make that more clear anyway.)

@APshenkin
Copy link
Author

APshenkin commented Dec 23, 2025

@charmander

I would really appreciate to get a feedback first, that those changes are in right direction. Those two changes are tight together as they both are related to serialization issue. Just because I solved global writer issue first, zombie client appeared next in PgPool, because without fixing writers you just write sync/close in next query

I'm not an expert of pg protocol and just investigated this issue few hours.
Maybe there were a reasons to use shared writers? If not and those changes makes sence, would be happy to do what you request

@charmander
Copy link
Collaborator

Yes, these changes make sense. The shared writer was probably an attempt at improving performance.

@brianc
Copy link
Owner

brianc commented Jan 14, 2026

  • Refactor: Move Writer instantiation to improve isolation and prevent state reuse issues.

If this is something which can be acomplished without reinstantiating the writer I would greatly prefer it. This is extremely hot path code. Is there a way to fix this w/o? Also: needs tests before merging.

Thanks for the eyes on this tho! I'll leave it open for a bit as it might be something I can quickly fix (IF there is a repoducable test case on how to trigger the issue)

@APshenkin
Copy link
Author

APshenkin commented Feb 11, 2026

@charmander @brianc

Sorry, I missed these comments, my bad.

I'll try to take a look on this back in a next few weeks (split to different commits, adding tests)

Re

If this is something which can be acomplished without reinstantiating the writer I would greatly prefer it.

I think it should be doable without reinstantiating the writer, however the fix will be fragile then for any future changes. Also code will be a bit ugly (each time when you have some error you should not forget to clean it properly). I'll try to play around it and come back with something

@brianc
Copy link
Owner

brianc commented Mar 5, 2026

I think it should be doable without reinstantiating the writer, however the fix will be fragile then for any future changes. Also code will be a bit ugly (each time when you have some error you should not forget to clean it properly). I'll try to play around it and come back with something

sounds good - i think we can just re-instantiate it then. allocating something is pretty cheap.

@brianc
Copy link
Owner

brianc commented Mar 5, 2026

Is it possible to write a couple tests for this? I'd like to get it merged.

…er in bind()

When valueMapper throws during serialize.bind(), the module-level
singleton Writer instances (writer and paramWriter) are left with
partial data from the interrupted operation. This corrupts all
subsequent serializer calls since they share the same Writer instances.

Add Writer.clear() method that resets the write cursor without
allocating a new buffer (zero overhead on the happy path). Wrap
writeValues() in bind() with try-catch to clear both writers on error.
When connection.bind() throws during prepare(), the catch block
previously called handleError() without sending any protocol messages.
Since PARSE was already buffered (due to cork), the server processes
it and waits for more messages, leaving the connection hung.

Send Close to clean up the parsed statement and Sync to return the
connection to a usable state.
@APshenkin APshenkin force-pushed the fix/fix-params-parsing-exceptions branch from 7c6914f to 0e0ff2a Compare March 19, 2026 15:55
@APshenkin
Copy link
Author

@brianc

I implemented change in a way to not reinstantiate writers, added tests for both issues and split two fixes into two different commits. Let me know if that will work for you

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.

Exception in values serialisation causes next query to use invalid statements.

3 participants