Skip to content

Conversation

@nightscape
Copy link
Contributor

Hi, while using Turso in an app I'm just developing, I ran into a few errors concerning statement lifecycle and transaction behaviour. Part of this fix is porting #4038 to the Rust bindings.
For full transparency: The fix was created by Opus 4.5 because I don't understand MVCC deeply enough.
I just gave it the failing code, had it create test cases and then implement a fix and verify that it works in my project.
I don't know if it's the right way to solve these issues.

Summary

When a prepared statement is reset or dropped, it now runs to completion first. This ensures INSERT...RETURNING and similar statements complete all their work even if the caller only consumed some of the returned rows.

Problem

Previously, if you did:

let mut stmt = conn.prepare("INSERT INTO t VALUES (1), (2), (3) RETURNING x").await?;
let mut rows = stmt.query(()).await?;
let first = rows.next().await?; // Only consume first row
drop(rows);
drop(stmt);  // Statement dropped without consuming all rows

Only the first row would be inserted because the statement wasn't run to completion.

Solution

In reset_internal(), run the statement to completion before calling abort(). This matches SQLite's behavior and ensures all side effects complete.

Uses catch_unwind to handle panics gracefully (e.g., from corrupted databases).

Tests

Added regression tests covering:

  • Partial RETURNING consumption
  • Transaction commit/rollback with non-MVCC mode
  • Combination of partial RETURNING and explicit transactions

@nightscape nightscape marked this pull request as ready for review November 26, 2025 08:38
@nightscape nightscape force-pushed the fix-statement-reset-and-transaction-commit branch from 819750b to 9de4011 Compare November 26, 2025 08:43
@jussisaurio
Copy link
Collaborator

Hi @nightscape , can you see if this change in core already fixes the same issue? #4038

@nightscape
Copy link
Contributor Author

@jussisaurio actually this PR builds on #4038 and applies a similar fix for the Rust bindings that the other PR does for the SQLite C-bindings.

Copilot AI review requested due to automatic review settings December 6, 2025 10:45
@nightscape nightscape force-pushed the fix-statement-reset-and-transaction-commit branch from 9de4011 to de4a61f Compare December 6, 2025 10:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request implements a fix to ensure statements run to completion when reset or dropped, addressing issues with INSERT...RETURNING statements and transaction behavior in non-MVCC mode. The core change adds a completion loop in reset_internal() that steps through any remaining statement execution before cleanup. The PR includes comprehensive test coverage for the new behavior.

Key Changes

  • Modified reset_internal() to run statements to completion before aborting
  • Added integration tests for non-MVCC transaction commit/rollback behavior
  • Added async tests for INSERT...RETURNING partial consumption scenarios

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
core/lib.rs Adds completion loop in reset_internal() to ensure statements finish executing before cleanup
tests/integration/query_processing/test_transactions.rs Adds synchronous tests for commit/rollback in non-MVCC mode
bindings/rust/tests/integration_tests.rs Adds async tests for INSERT...RETURNING partial consumption and transaction interactions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@nightscape nightscape force-pushed the fix-statement-reset-and-transaction-commit branch 3 times, most recently from 3ce0ad0 to 95445b9 Compare December 6, 2025 13:17
@nightscape
Copy link
Contributor Author

@jussisaurio your and copilot's comments should be addressed now.

@nightscape nightscape force-pushed the fix-statement-reset-and-transaction-commit branch from 95445b9 to 911f791 Compare December 8, 2025 10:30
@nightscape nightscape force-pushed the fix-statement-reset-and-transaction-commit branch from 911f791 to 5c38e93 Compare December 9, 2025 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants