Skip to content

Commit c8ed6af

Browse files
committed
test: Make sure the close-db-on-failure branch has coverage
Specifically I want valgrind to be checking for correctness. Also add a changelog entry.
1 parent 945a606 commit c8ed6af

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# sqlite3-ruby Changelog
22

3+
## next / unreleased
4+
5+
### Improved
6+
7+
- When `Database.new` fails to open the database file, the underlying sqlite3 connection handle is now closed immediately instead of waiting for the garbage collector to clean it up. #719 @katafrakt
8+
9+
310
## 2.9.5 / 2026-06-07
411

512
### Dependencies

test/test_resource_cleanup.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@ def test_cleanup_unclosed_statement_object
1717
end
1818
end
1919

20+
# These tests exercise the failure branches of open_v2 and open16, which immediately close the
21+
# sqlite3 handle and reclaim resources before raising.
22+
#
23+
# Under valgrind these would catch any memory problems in the close-on-failure code, but they do
24+
# NOT directly test that resources are immediately recovered. If we didn't close immediately,
25+
# the GC finalizer would still eventually close any leftover handles if and when the database
26+
# object is GCed. Beacuse we run ruby with free-at-exit with valgrind, directly testing the
27+
# immediate close is hard. But I'm fine with that tradeoff.
28+
def test_cleanup_failed_open_v2
29+
exception = assert_raise(SQLite3::CantOpenException) do
30+
SQLite3::Database.new("/nonexistent/foo.db")
31+
end
32+
assert_equal("unable to open database file", exception.message)
33+
end
34+
35+
def test_cleanup_failed_open16
36+
exception = assert_raise(SQLite3::CantOpenException) do
37+
SQLite3::Database.new("/nonexistent/foo.db".encode(Encoding::UTF_16LE))
38+
end
39+
assert_equal("unable to open database file", exception.message)
40+
end
41+
2042
# # this leaks the result set
2143
# def test_cleanup_unclosed_resultset_object
2244
# db = SQLite3::Database.new(':memory:')

0 commit comments

Comments
 (0)