Thread confinement for Appender (1.5)#585
Merged
staticlibs merged 1 commit intoduckdb:v1.5-variegatafrom Feb 28, 2026
Merged
Conversation
This is a backport of the PR duckdb#583 to `v1.5-variegata` stable branch. Appender instances are not thread-safe. Only the `close()` method can be safely called from other threads concurrently with other operations. `append()` and `flush()` operations operate on the same native buffer and cannot be called concurrently. This PR implements a variant of a thread confinement for the Appender class intances. Only the thread that has created the Appender can call its methods (`close()` method still can be called from any thread). Method calls with throw `SQLExeption`s when called from other threads. When it is necessary to use Appender from multiple threads, it is required to call `unsafeBreakThreadConfinement()` method first and use a `Lock` instance returned from it to synchronize the access to this Appender instance. Example: ```java try (DuckDBAppender appender = connection.createAppender("tab1")) { Thread th = new Thread(() -> { // appender.flush(); // throws SQLException Lock appenderLock = appender.unsafeBreakThreadConfinement(); appenderLock.lock(); try { appender.flush(); } catch (SQLException e) { e.printStackTrace(); } finally { appenderLock.unlock(); } }); th.start(); th.join(); } ``` Testing: a concurrent test added that, without the patch, was crashing the JVM in about 1 of 10 runs. Fixes: duckdb#582
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.
This is a backport of the PR #583 to
v1.5-variegatastable branch.Appender instances are not thread-safe. Only the
close()method can be safely called from other threads concurrently with other operations.append()andflush()operations operate on the same native buffer and cannot be called concurrently.This PR implements a variant of a thread confinement for the Appender class intances. Only the thread that has created the Appender can call its methods (
close()method still can be called from any thread). Method calls with throwSQLExeptions when called from other threads.When it is necessary to use Appender from multiple threads, it is required to call
unsafeBreakThreadConfinement()method first and use aLockinstance returned from it to synchronize the access to this Appender instance.Example:
Testing: a concurrent test added that, without the patch, was crashing the JVM in about 1 of 10 runs.
Fixes: #582