Repository navigation
sql(postgres): discard a partial Bind when a parameter fails to encode #34732
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
Changes from 5 commits
ad37b23
7eb7910
5b4b162
298c1e9
85f6362
e770376
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 |
|---|---|---|
|
|
@@ -292,66 +292,70 @@ pub(crate) fn prepare_and_query_with_signature<Context: WriterContext>( | |
| global: &JSGlobalObject, | ||
| query: &[u8], | ||
| array_value: JSValue, | ||
| mut writer: protocol::NewWriter<Context>, | ||
| writer: protocol::NewWriter<Context>, | ||
| signature: &mut Signature, | ||
| ) -> Result<(), AnyPostgresError> { | ||
| write_query( | ||
| query, | ||
| &signature.prepared_statement_name, | ||
| &signature.fields, | ||
| writer, | ||
| )?; | ||
| write_bind( | ||
| &signature.prepared_statement_name, | ||
| &BunString::EMPTY, | ||
| global, | ||
| array_value, | ||
| JSValue::ZERO, | ||
| &[], | ||
| &[], | ||
| writer, | ||
| )?; | ||
| let exec = protocol::Execute { | ||
| p: protocol::PortalOrPreparedStatement::PreparedStatement( | ||
| writer.atomically(|mut writer| { | ||
| write_query( | ||
| query, | ||
| &signature.prepared_statement_name, | ||
| ), | ||
| ..Default::default() | ||
| }; | ||
| exec.write_internal(&mut writer)?; | ||
| &signature.fields, | ||
| writer, | ||
| )?; | ||
| write_bind( | ||
| &signature.prepared_statement_name, | ||
| &BunString::EMPTY, | ||
| global, | ||
| array_value, | ||
| JSValue::ZERO, | ||
| &[], | ||
| &[], | ||
| writer, | ||
| )?; | ||
| let exec = protocol::Execute { | ||
| p: protocol::PortalOrPreparedStatement::PreparedStatement( | ||
| &signature.prepared_statement_name, | ||
| ), | ||
| ..Default::default() | ||
| }; | ||
| exec.write_internal(&mut writer)?; | ||
|
|
||
| writer.write(&protocol::FLUSH)?; | ||
| writer.write(&protocol::SYNC)?; | ||
| Ok(()) | ||
| writer.write(&protocol::FLUSH)?; | ||
| writer.write(&protocol::SYNC)?; | ||
| Ok(()) | ||
| }) | ||
| } | ||
|
|
||
| pub(crate) fn bind_and_execute<Context: WriterContext>( | ||
| global: &JSGlobalObject, | ||
| statement: &PostgresSQLStatement, | ||
| array_value: JSValue, | ||
| columns_value: JSValue, | ||
| mut writer: protocol::NewWriter<Context>, | ||
| writer: protocol::NewWriter<Context>, | ||
| ) -> Result<(), AnyPostgresError> { | ||
| write_bind( | ||
| &statement.signature.prepared_statement_name, | ||
| &BunString::EMPTY, | ||
| global, | ||
| array_value, | ||
| columns_value, | ||
| &statement.parameters, | ||
| &statement.fields, | ||
| writer, | ||
| )?; | ||
| let exec = protocol::Execute { | ||
| p: protocol::PortalOrPreparedStatement::PreparedStatement( | ||
| writer.atomically(|mut writer| { | ||
| write_bind( | ||
| &statement.signature.prepared_statement_name, | ||
| ), | ||
| ..Default::default() | ||
| }; | ||
| exec.write_internal(&mut writer)?; | ||
| &BunString::EMPTY, | ||
| global, | ||
| array_value, | ||
| columns_value, | ||
| &statement.parameters, | ||
| &statement.fields, | ||
| writer, | ||
| )?; | ||
| let exec = protocol::Execute { | ||
| p: protocol::PortalOrPreparedStatement::PreparedStatement( | ||
| &statement.signature.prepared_statement_name, | ||
| ), | ||
| ..Default::default() | ||
| }; | ||
| exec.write_internal(&mut writer)?; | ||
|
|
||
| writer.write(&protocol::FLUSH)?; | ||
| writer.write(&protocol::SYNC)?; | ||
| Ok(()) | ||
| writer.write(&protocol::FLUSH)?; | ||
| writer.write(&protocol::SYNC)?; | ||
| Ok(()) | ||
| }) | ||
| } | ||
|
|
||
| /// Atomically sends Parse + [Describe] + Bind + Execute + Flush + Sync as a single message batch. | ||
|
|
@@ -366,61 +370,63 @@ pub(crate) fn parse_and_bind_and_execute<Context: WriterContext>( | |
| array_value: JSValue, | ||
| columns_value: JSValue, | ||
| include_describe: bool, | ||
| mut writer: protocol::NewWriter<Context>, | ||
| writer: protocol::NewWriter<Context>, | ||
| ) -> Result<(), AnyPostgresError> { | ||
| let name = &statement.signature.prepared_statement_name; | ||
|
|
||
| // Parse | ||
| { | ||
| let q = protocol::Parse { | ||
| name, | ||
| params: &statement.signature.fields, | ||
| query, | ||
| writer.atomically(|mut writer| { | ||
|
Contributor
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. 🔴 With Why this was flaggedA connection created with prepare: false (ConnectionFlags::USE_UNNAMED_PREPARED_STATEMENTS) runs one query with a parameter whose encoding fails (throwing toString, number[] to bytea) while no other query is in flight, e.g. the last statement of a CLI script that does not call sql.close(). PostgresSQLQuery.rs:693-769 skips writing for unnamed statements with params, pushes the request at :810, refs poll_ref at :817, then calls connection.advance_and_flush() at :832. advance() at PostgresSQLConnection.rs:2183 calls parse_and_bind_and_execute; the closure at PostgresRequest.rs:377 fails, NewWriter.rs:75-76 truncates the buffer to its start, the request is rejected via req.on_js_error at :2194 and popped by discard_request at :2207; flush_data at :1837 finds an empty buffer. poll_ref is only unref'd in on_data's idle block (PostgresSQLConnection.rs:1053-1062), on close/failure (:814, :1413, :1509) or by an explicit unref(); none of those runs because no bytes were sent and no reply arrives, and idleTimeout defaults to 0 (shared.ts:878) so no timer closes the connection. The process therefore… Verification: normal — when a |
||
| // Parse | ||
| { | ||
| let q = protocol::Parse { | ||
| name, | ||
| params: &statement.signature.fields, | ||
| query, | ||
| }; | ||
| q.write_internal(&mut writer)?; | ||
| bun_core::scoped_log!(Postgres, "Parse: {}", bun_fmt::quote(query)); | ||
| } | ||
|
|
||
| // Describe (needed on first execution to learn parameter/result types for caching) | ||
| if include_describe { | ||
| let d = protocol::Describe { | ||
| p: protocol::PortalOrPreparedStatement::PreparedStatement(name), | ||
| }; | ||
| d.write_internal(writer)?; | ||
| bun_core::scoped_log!(Postgres, "Describe: {}", bun_fmt::quote(name)); | ||
| } | ||
|
|
||
| // Bind — use server-provided types if available (binary format), otherwise | ||
| // fall back to signature types (text format for unknowns). The server will | ||
| // handle text-to-type conversion based on the parameter types from Parse. | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
| let param_fields = if !statement.parameters.is_empty() { | ||
| &statement.parameters[..] | ||
| } else { | ||
| &statement.signature.fields[..] | ||
| }; | ||
| q.write_internal(&mut writer)?; | ||
| bun_core::scoped_log!(Postgres, "Parse: {}", bun_fmt::quote(query)); | ||
| } | ||
| let result_fields = &statement.fields; | ||
|
|
||
| // Describe (needed on first execution to learn parameter/result types for caching) | ||
| if include_describe { | ||
| let d = protocol::Describe { | ||
| write_bind( | ||
| name, | ||
| &BunString::EMPTY, | ||
| global, | ||
| array_value, | ||
| columns_value, | ||
| param_fields, | ||
| result_fields, | ||
| writer, | ||
| )?; | ||
|
|
||
| // Execute | ||
| let exec = protocol::Execute { | ||
| p: protocol::PortalOrPreparedStatement::PreparedStatement(name), | ||
| ..Default::default() | ||
| }; | ||
| d.write_internal(writer)?; | ||
| bun_core::scoped_log!(Postgres, "Describe: {}", bun_fmt::quote(name)); | ||
| } | ||
| exec.write_internal(&mut writer)?; | ||
|
|
||
| // Bind — use server-provided types if available (binary format), otherwise | ||
| // fall back to signature types (text format for unknowns). The server will | ||
| // handle text-to-type conversion based on the parameter types from Parse. | ||
| let param_fields = if !statement.parameters.is_empty() { | ||
| &statement.parameters[..] | ||
| } else { | ||
| &statement.signature.fields[..] | ||
| }; | ||
| let result_fields = &statement.fields; | ||
|
|
||
| write_bind( | ||
| name, | ||
| &BunString::EMPTY, | ||
| global, | ||
| array_value, | ||
| columns_value, | ||
| param_fields, | ||
| result_fields, | ||
| writer, | ||
| )?; | ||
|
|
||
| // Execute | ||
| let exec = protocol::Execute { | ||
| p: protocol::PortalOrPreparedStatement::PreparedStatement(name), | ||
| ..Default::default() | ||
| }; | ||
| exec.write_internal(&mut writer)?; | ||
|
|
||
| writer.write(&protocol::FLUSH)?; | ||
| writer.write(&protocol::SYNC)?; | ||
| Ok(()) | ||
| writer.write(&protocol::FLUSH)?; | ||
| writer.write(&protocol::SYNC)?; | ||
| Ok(()) | ||
| }) | ||
| } | ||
|
|
||
| pub(crate) fn execute_query<Context: WriterContext>( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.