HaxeFoundation / neko

The Neko Virtual Machine
https://nekovm.org
Other
552 stars 107 forks source link

Keep reference to k_db abstract and avoid early finalizer call #200

Closed jonasmalacofilho closed 7 months ago

jonasmalacofilho commented 5 years ago

In the SQLite CFFI library, connect returns an abstract Neko value, of k_db kind and with a finalizer set. If the k_result request value only keeps a pointer to the db struct, instead of the corresponding k_db value, the finalizer will be called as soon as no other external references remain to that connection, even if the request itself is still reachable.

The issue could manifest in code like (Haxe):

var rs = Sqlite.open("db.db").request("select * from tbl");
trace(rs.length);

Related: HaxeFoundation/haxe#8728

ncannasse commented 5 years ago

Should we should check if val_db(r->database) == NULL since the database might have been closed in the meanwhile

jonasmalacofilho commented 5 years ago

@ncannasse maybe, where exactly do you mean?

One thing I noticed is that free_db will set r->done early on, and that cleanly stops result_next. In fact, that's why we had no exception or segfault even though the db had been closed and freed.

Simn commented 7 months ago

I was told to merge this!