duckdb / duckdb-java

DuckDB JDBC Driver
https://duckdb.org/docs/api/java.html
MIT License
42 stars 23 forks source link

JDBC: Undefined behavior when reading from a returned `java.sql.Blob` outside of a closed result set #99

Closed ShadelessFox closed 2 weeks ago

ShadelessFox commented 1 month ago

What happens?

Blobs retrieved from a result set before it's closed might return random values between GC calls.

To Reproduce

import java.sql.*;
import java.util.HexFormat;

public class DuckDBBlobTest {
    public static void main(String[] args) throws Exception {
        try (Connection connection = DriverManager.getConnection("jdbc:duckdb:")) {
            try (Statement stmt = connection.createStatement()) {
                Blob blob;

                try (ResultSet resultSet = stmt.executeQuery("select 'AAAA'::blob")) {
                    resultSet.next();
                    blob = resultSet.getBlob(1);
                }

                String expected = HexFormat.of().formatHex(new byte[]{'A', 'A', 'A', 'A'});
                for (int i = 0; i < 5000; i++) {
                    System.gc();
                    String actual = HexFormat.of().formatHex(blob.getBytes(0, Math.toIntExact(blob.length())));
                    if (!expected.equals(actual)) {
                        throw new SQLException("expected != actual: " + expected + " != " + actual);
                    }
                }
            }
        }
    }
}

Please note that this is a floating issue and might not reproduce immediately. Try running the sample multiple times. Eventually, you will see something like this:

expected != actual: 41414141 != 00000000

OS:

Windows 11 x64

DuckDB Version:

1.1.1

DuckDB Client:

Java JDBC / DBeaver 24.2.3

Hardware:

No response

Full Name:

Ian Vinogradov

Affiliation:

DBeaver

What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have tested with a stable release

Did you include all relevant data sets for reproducing the issue?

Yes

Did you include all code required to reproduce the issue?

Did you include all relevant configuration (e.g., CPU architecture, Python version, Linux distribution) to reproduce the issue?

Tishj commented 1 month ago

@szarnyasg can we move this to https://github.com/duckdb/duckdb-java ?

EastLord commented 2 weeks ago

https://github.com/duckdb/duckdb-java/issues/79

Mause commented 2 weeks ago

Going to assume this is resolved by #103