Skip to content

Commit cd49a6b

Browse files
committed
Drop libsql::Database on close() to release file locks
Make db field Optional so close() can set it to None, which drops the libsql::Database and releases underlying file handles immediately. Previously only the connection was dropped, leaving the database object alive until GC — causing EBUSY on Windows when trying to delete the database file right after close().
1 parent 57b39a6 commit cd49a6b

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/lib.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ struct AccessMode {
217217
#[napi]
218218
pub struct Database {
219219
// The libSQL database instance.
220-
db: libsql::Database,
220+
db: Option<libsql::Database>,
221221
// The libSQL connection instance.
222222
conn: Option<Arc<libsql::Connection>>,
223223
// Whether to use safe integers by default.
@@ -229,6 +229,7 @@ pub struct Database {
229229
impl Drop for Database {
230230
fn drop(&mut self) {
231231
self.conn = None;
232+
self.db = None;
232233
}
233234
}
234235

@@ -321,7 +322,7 @@ pub async fn connect(path: String, opts: Option<Options>) -> Result<Database> {
321322
.map_err(Error::from)?
322323
}
323324
Ok(Database {
324-
db,
325+
db: Some(db),
325326
conn: Some(Arc::new(conn)),
326327
default_safe_integers,
327328
memory,
@@ -493,7 +494,11 @@ impl Database {
493494
/// The maximum write replication index.
494495
#[napi]
495496
pub fn max_write_replication_index(&self) -> Result<f64> {
496-
let result = self.db.max_write_replication_index();
497+
let db = match &self.db {
498+
Some(db) => db,
499+
None => return Ok(0.0),
500+
};
501+
let result = db.max_write_replication_index();
497502
Ok(result.unwrap_or(0) as f64)
498503
}
499504

@@ -526,7 +531,17 @@ impl Database {
526531
/// A `SyncResult` instance.
527532
#[napi]
528533
pub async fn sync(&self) -> Result<SyncResult> {
529-
let result = self.db.sync().await.map_err(Error::from)?;
534+
let db = match &self.db {
535+
Some(db) => db,
536+
None => {
537+
return Err(throw_sqlite_error(
538+
"The database connection is not open".to_string(),
539+
"SQLITE_NOTOPEN".to_string(),
540+
0,
541+
));
542+
}
543+
};
544+
let result = db.sync().await.map_err(Error::from)?;
530545
Ok(SyncResult {
531546
frames_synced: result.frames_synced() as f64,
532547
replication_index: result.frame_no().unwrap_or(0) as f64,
@@ -552,6 +567,7 @@ impl Database {
552567
#[napi]
553568
pub fn close(&mut self) -> Result<()> {
554569
self.conn = None;
570+
self.db = None;
555571
Ok(())
556572
}
557573

0 commit comments

Comments
 (0)