Exclude server_version from Synchronization mutex#288
Merged
byroot merged 1 commit intotrilogy-libraries:mainfrom Apr 1, 2026
Merged
Conversation
server_version is a pure memory read that returns a fixed char[] copied from the MySQL handshake greeting packet at connect time. It performs no socket I/O and its value is immutable for the lifetime of the connection. This makes it identical in safety characteristics to closed?, which is already excluded from synchronization (see trilogy-libraries#277). Wrapping server_version in the Synchronization mutex causes SynchronizationError when it is called re-entrantly from within another synchronized method. This happens in practice because OpenTelemetry instrumentation wraps Trilogy#query in a span and calls server_version inside the span block to record the server version as a span attribute. Since query is synchronized, calling server_version from within it re-enters the mutex and raises SynchronizationError. The C implementation confirms there is no socket interaction: static VALUE rb_trilogy_server_version(VALUE self) { return rb_str_new_cstr(get_open_ctx(self)->server_version); } The server_version field is written once during _connect: memcpy(ctx->server_version, handshake.server_version, TRILOGY_SERVER_VERSION_SIZE); ctx->server_version[TRILOGY_SERVER_VERSION_SIZE] = 0; It is never modified again. There is no risk of data races or protocol corruption from concurrent reads of this field.
957e71b to
a8d4aa1
Compare
composerinteralia
approved these changes
Apr 1, 2026
Collaborator
composerinteralia
left a comment
There was a problem hiding this comment.
Makes sense to me. Thank you
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.
Problem
Trilogy#server_versionis wrapped by theSynchronizationmutex, but it is a pure memory read with no socket I/O. This causesSynchronizationErrorwhen instrumentation libraries callserver_versionfrom within another synchronized method.In practice, OpenTelemetry instrumentation (private to our company) wraps
Trilogy#queryin a span and callsserver_versioninside the span block to record the server version as a span attribute. Sincequeryis synchronized, callingserver_versionfrom within it re-enters the mutex and raisesSynchronizationError:This is the same class of problem as #276, which was fixed for
closed?in #277.Why
server_versionis safe to excludeserver_versionreturns a fixedchar[]copied from the MySQL handshake greeting packet at connect time. It is never modified after initialization and performs no socket I/O.The C implementation:
The field is written once during
_connectand never again:This makes it identical in safety characteristics to
closed?, which is already excluded