Skip to content

Failed SQLite opens leave native connection handles allocated #273

Closed

Description

@dimensionscape

Repeated failed SQLite opens leak the native connection allocated by sqlite3_open. There is no managed wrapper on this path, so GC cannot clean it up.

Reproduced on main at d734565 with Haxe 4.3.7, Windows x86-64, MSVC 19.44.35213, HXCPP_M64 and smart strings enabled. The tests use an isolated local haxelib repository pointing at this checkout.

The reproducer opens a database under a directory that does not exist. After one warm-up failure, it repeats 128 failing opens and runs three full collections. SQLite's own sqlite3_memory_used counter is 174,080 bytes above its baseline, or 1,360 bytes per failed open on this build. Every open correctly throws, but the memory remains allocated.

In src/hx/libs/sqlite/Sqlite.cpp, _hx_sqlite_connect calls sqlite3_open and immediately calls sqlite_error on failure. The database wrapper and its finalizer are created only after success. The failure path never closes sqlDb.

The bundled sqlite3.h documents that sqlite3_open usually returns a connection handle even on failure, and that this handle must be passed to sqlite3_close whether opening succeeds or fails.

Close a non-null failure handle while preserving the original error message. Cover missing directories and invalid database paths, and keep successful open/close behavior unchanged. This is a runtime-only resource cleanup fix, with no compiler or standard library change.

A conventional regression is prepared locally at test/regression/SqliteFailedOpenMemory, with build.hxml and stdout.txt. It uses sqlite3_memory_used rather than process RSS, warms up SQLite before measuring, and verifies that all 128 opens fail. Current output:

failed opens: 128
failed open memory released: false

The last line should be true. Main.hx:

@:cppFileCode('
extern "C" long long sqlite3_memory_used(void);
')
class Main {
	static function failOpen(path:String):Bool {
		try {
			var db = sys.db.Sqlite.open(path);
			db.close();
			return false;
		} catch (e:Dynamic) {
			return true;
		}
	}
	static function main() {
		var parent = "missing-sqlite-parent-" + Std.string(Date.now().getTime());
		if (sys.FileSystem.exists(parent)) throw "Fixture directory already exists";
		var path = haxe.io.Path.join([Sys.getCwd(), parent, "data.sqlite"]);
		if (!failOpen(path)) throw "Open unexpectedly succeeded";
		cpp.vm.Gc.run(true);
		var before:Float = untyped __cpp__("(double)sqlite3_memory_used()");
		var failures = 0;
		for (i in 0...128) if (failOpen(path)) failures++;
		for (i in 0...3) cpp.vm.Gc.run(true);
		var after:Float = untyped __cpp__("(double)sqlite3_memory_used()");
		Sys.println("failed opens: " + failures);
		Sys.println("failed open memory released: " + (after - before < 4096));
	}
}

Build with haxe build.hxml -D HXCPP_M64 and run bin/Main from the regression directory after pointing haxelib at this checkout. The temporary failed-open path is never created. The counter assertion assumes SQLite memory-status tracking is enabled, as in the bundled default build.

Activity

  1. dimensionscape commented on Oct 24, 2026

    @dimensionscape
    MemberAuthor

    Fixed in d563fb720c31a29d284044b0556da43f88aeef57. When sqlite3_open fails it still allocates a handle, which is now closed before the error is thrown. The error message is unchanged. Test: SqliteFailedOpenMemory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions