0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-18 19:33:00 -05:00

fix(ext/node): throw Session methods when database is closed (#27968)

This commit is contained in:
Divy Srivastava 2025-02-06 08:52:49 +05:30 committed by GitHub
parent ece384c094
commit 28faaee772
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 2 deletions

View file

@ -376,7 +376,7 @@ impl DatabaseSync {
Ok(Session {
inner: raw_session,
freed: Cell::new(false),
_db: self.conn.clone(),
db: self.conn.clone(),
})
}
}

View file

@ -24,7 +24,7 @@ pub struct Session {
pub(crate) freed: Cell<bool>,
// Hold a strong reference to the database.
pub(crate) _db: Rc<RefCell<Option<rusqlite::Connection>>>,
pub(crate) db: Rc<RefCell<Option<rusqlite::Connection>>>,
}
impl GarbageCollected for Session {}
@ -57,6 +57,10 @@ impl Session {
// Closes the session.
#[fast]
fn close(&self) -> Result<(), SqliteError> {
if self.db.borrow().is_none() {
return Err(SqliteError::AlreadyClosed);
}
self.delete()
}
@ -66,6 +70,9 @@ impl Session {
// This method is a wrapper around `sqlite3session_changeset()`.
#[buffer]
fn changeset(&self) -> Result<Box<[u8]>, SqliteError> {
if self.db.borrow().is_none() {
return Err(SqliteError::AlreadyClosed);
}
if self.freed.get() {
return Err(SqliteError::SessionClosed);
}
@ -78,6 +85,9 @@ impl Session {
// This method is a wrapper around `sqlite3session_patchset()`.
#[buffer]
fn patchset(&self) -> Result<Box<[u8]>, SqliteError> {
if self.db.borrow().is_none() {
return Err(SqliteError::AlreadyClosed);
}
if self.freed.get() {
return Err(SqliteError::SessionClosed);
}

View file

@ -96,6 +96,9 @@ Deno.test("[node/sqlite] createSession and changesets", () => {
assertThrows(() => session.changeset(), Error, "Session is already closed");
// Close after close should throw.
assertThrows(() => session.close(), Error, "Session is already closed");
db.close();
assertThrows(() => session.close(), Error, "Database is already closed");
});
Deno.test("[node/sqlite] StatementSync integer too large", () => {