Skip to content

Non-owning connection clears up hooks #1784

Description

@marco6

Hi! I've run in an issue when dealing with a raw handle (I'm in a situation similar to #1477, where I have a raw handle and I need to query): rusqlite will remove all hooks when the non owning connection is dropped.

A small reproducer could be something on the lines of:

use rusqlite::{Connection, params};

fn main() {
    let conn = Connection::open_in_memory().unwrap();
    conn.commit_hook(Some(|| {
        println!("committed!");
        false
    })).unwrap();

    conn.execute("CREATE TABLE test(value)", params![]).unwrap(); // Prints "committed!"

    {
        let conn2 = unsafe { Connection::from_handle(conn.handle()) }.unwrap();
        conn2.execute("INSERT INTO test(value) VALUES ('Test');", params![]).unwrap(); // Prints "committed!"
    }

    conn.execute("DROP TABLE test", params![]).unwrap(); // Doesn't print anything
}

Notice also that the original conn object is still keeping the boxed hook alive.

I do understand that conn2 needs to clean up hooks in case it registered them, otherwise it would create a use-after-free, but maybe it shouldn't clean them up when they weren't set?

If my reasoning above is sound, I can quickly post a PR to fix this behavior. Any feedback would be nice!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions