Open rsk700 opened 3 years ago
I also see this, and can reproduce it easily.
As far as I can tell, the iteration actually works fine, everything works fine, and the crash occurs when closing the ResultSet.
Is there any workaround that'd still let me query the database inside of an iteration over the results of a query? I have a use case where I'm iterating hundreds of thousands of rows, and don't want to load them all into memory, or take the performance hit of using LIMIT/OFFSET to "page" through the results.
I think I have found a workaround that usually works. By ensuring the connection pool has at least two members, and checking out a connection outside of the main query, but using the checked-out connection for the read-only query inside the loop, it doesn't seem to fail.
However, with an in-memory database, it fails to see the table that was created. So I am using an on-disk file in this test:
require "sqlite3"
DB.open "sqlite3://./test.sqlite3?max_idle_pool_size=2&initial_pool_size=2" do |db|
db.exec "create table t1(id integer primary key autoincrement, t text)"
db.exec "insert into t1 (t) values ('v1')"
db.using_connection do |connection|
db.query "select id, t from t1" do |rs|
rs.each do
v = connection.scalar "select id from t1 limit 1"
end
end
end
end
This seems to work pretty consistently.
Crystal 1.0.0 [dd40a2442] (2021-03-22) LLVM: 10.0.0 Default target: x86_64-unknown-linux-gnu sqlite3: version: 0.18.0
Running this code with query inside iteration, fails with
Invalid memory access
error:and error: