-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Fix: Replace unsafe unwrap in Postgres connector to prevent panic #14010
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: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @Yuanjia-Lee, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the robustness of the application by addressing a critical stability issue in the Postgres data connector. By transitioning from direct Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request correctly identifies a critical stability issue and takes the right step by replacing .unwrap() with error propagation using the ? operator. This is an excellent improvement to prevent the service from panicking due to database connection or query failures.
However, the changes as proposed will likely not compile. The ? operator requires an automatic conversion between error types (from deadpool_postgres::PoolError and tokio_postgres::Error to your ConversationStorageError), which does not appear to be defined. I've left specific comments with suggestions to fix this using .map_err(). For a cleaner, more idiomatic solution across the file, I recommend implementing the From trait on your storage error enums for these external error types.
More importantly, this file (postgres.rs) contains numerous other .unwrap() and .expect() calls that present the same panic risk. While this PR is a great start, I strongly recommend a follow-up effort to eliminate all remaining instances of unwrap() and expect() in this connector to make it fully robust.
|
|
||
| async fn initialize_schema(store: PostgresStore) -> Result<(), ConversationStorageError> { | ||
| let client = store.pool.get().await.unwrap(); | ||
| let client = store.pool.get().await?; |
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.
This change correctly identifies the need for error propagation, but it will likely cause a compilation error. The ? operator requires a conversion from deadpool_postgres::PoolError (the error type from pool.get()) to this function's error type, ConversationStorageError. Since there doesn't appear to be an impl From<deadpool_postgres::PoolError> for ConversationStorageError, the conversion fails.
A more idiomatic Rust solution would be to implement the From trait. However, for a localized fix, you can use map_err to perform the conversion explicitly.
| let client = store.pool.get().await?; | |
| let client = store.pool.get().await.map_err(|e| ConversationStorageError::StorageError(e.to_string()))?; |
| ) | ||
| .await | ||
| .unwrap(); | ||
| .await?; |
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.
Similar to the previous comment, this change will also likely fail to compile. The error from batch_execute is tokio_postgres::Error, which needs to be converted to ConversationStorageError for the ? operator to work.
You can fix this by explicitly mapping the error.
| .await?; | |
| .await.map_err(|e| ConversationStorageError::StorageError(e.to_string()))?; |
Motivation
This PR addresses a stability issue identified in the Postgres data connector where database failures could cause the Router process to panic.
Previously,
unwrap()was used directly onpool.get().awaitandclient.execute(...). If the database connection failed or a query error occurred (e.g., constraint violation), the unhandledErrwould crash the entire service.Modifications
sgl-router/src/data_connector/postgres.rs:.unwrap()with the?operator forself.store.pool.get().await..unwrap()with the?operator forclient.execute(...).This change ensures that database errors are propagated up the call stack as
Result::Errinstead of triggering a panic, allowing the router to handle failures gracefully.Accuracy Tests
Benchmarking and Profiling
Checklist