-
Notifications
You must be signed in to change notification settings - Fork 205
Cache server_version_info and guard against re-entrant reads #622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -421,15 +421,23 @@ def has_sequence(self, connection: Connection, sequence_name: str, schema: str = | |
|
|
||
| @classmethod | ||
| def _get_server_version_info(cls, connection: Connection) -> Any: | ||
| def get_server_version_info(_): | ||
| def get_server_version_info(self): | ||
| if "_trino_server_version_info" in self.__dict__: | ||
| return self.__dict__["_trino_server_version_info"] | ||
| # Seed the cache before running the query so that a re-entrant read | ||
| # triggered while the version query is in flight returns immediately | ||
| # instead of recursing infinitely. | ||
| self.__dict__["_trino_server_version_info"] = None | ||
| query = "SELECT version()" | ||
| try: | ||
| res = connection.execute(sql.text(query)) | ||
| version = res.scalar() | ||
| return tuple([version]) | ||
| value = tuple([version]) | ||
| except exc.ProgrammingError as e: | ||
| logger.debug(f"Failed to get server version: {e.orig.message}") | ||
| return None | ||
| value = None | ||
| self.__dict__["_trino_server_version_info"] = value | ||
| return value | ||
|
|
||
| # Make server_version_info lazy in order to only make HTTP calls if user explicitly requests it. | ||
| cls.server_version_info = property(get_server_version_info, lambda instance, value: None) | ||
|
Comment on lines
423
to
443
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift Avoid mutating the class and capturing transient initialization connections. This lazy evaluation approach introduces three severe architectural hazards across SQLAlchemy engines and threads:
If the overhead of a single `@classmethod`
def _get_server_version_info(cls, connection: Connection) -> Any:
query = "SELECT version()"
try:
res = connection.execute(sql.text(query))
version = res.scalar()
return (version,)
except exc.ProgrammingError as e:
logger.debug("Failed to get server version: %s", e)
return None🤖 Prompt for AI Agents |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Crash risk and incorrect logging when accessing
e.orig.message.Accessing
e.orig.messageintroduces two significant failure modes:e.orig) does not have amessageattribute (which is common for standard DBAPI exceptions), this will raise anAttributeError. SinceAttributeErroris not caught here, it will bubble up and crash the application during the version lookup.TrinoQueryErrorwheremessageis defined as a method (def message(self) -> str:), omitting the parentheses causes the f-string to evaluate and log the bound method object (e.g.,<bound method ...>) instead of the actual error message.To fix both issues and robustly handle any underlying DBAPI exception type, use
str(e)oredirectly.🛠️ Proposed fix to safely log the exception
📝 Committable suggestion
🤖 Prompt for AI Agents