Skip to content

Commit

Permalink
Handle graciously failures on Close
Browse files Browse the repository at this point in the history
Needs first to avoid throwing in destructor (big no), AND to convert JS exception in C++ exception
More iteration on comments to duckdb#1856
  • Loading branch information
carlopi committed Jan 15, 2025
1 parent 7456b46 commit 0f29e56
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/include/duckdb/web/io/web_filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,13 @@ class WebFileSystem : public duckdb::FileSystem {
/// Delete copy constructor
WebFileHandle(const WebFileHandle &) = delete;
/// Destructor
virtual ~WebFileHandle() { Close(); }
virtual ~WebFileHandle() {
try {
Close();
} catch (...) {
// Avoid crashes if Close happens to throw
}
}
/// Get the file name
auto &GetName() const { return file_->file_name_; }
/// Resolve readahead
Expand Down
5 changes: 5 additions & 0 deletions packages/duckdb-wasm/src/bindings/runtime_browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ export const BROWSER_RUNTIME: DuckDBRuntime & {
closeFile: (mod: DuckDBModule, fileId: number) => {
const file = BROWSER_RUNTIME.getFileInfo(mod, fileId);
BROWSER_RUNTIME._fileInfoCache.delete(fileId);
try {
switch (file?.dataProtocol) {
case DuckDBDataProtocol.BUFFER:
case DuckDBDataProtocol.HTTP:
Expand All @@ -492,6 +493,10 @@ export const BROWSER_RUNTIME: DuckDBRuntime & {
return handle.flush();
}
}
} catch (e: any) {
console.log(e);
failWith(mod, e.toString());
}
},
dropFile: (mod: DuckDBModule, fileNamePtr: number, fileNameLen: number) => {
const fileName = readString(mod, fileNamePtr, fileNameLen);
Expand Down
1 change: 1 addition & 0 deletions packages/duckdb-wasm/test/opfs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export function testOPFS(baseDir: string, bundle: () => duckdb.DuckDBBundle): vo
await db.registerFileHandle('test.csv', testHandle, duckdb.DuckDBDataProtocol.BROWSER_FSACCESS, true);
await conn.send(`CREATE TABLE zzz AS SELECT * FROM "${baseDir}/tpch/0_01/parquet/lineitem.parquet"`);
await conn.send(`COPY (SELECT * FROM zzz) TO 'test.csv'`);
await conn.send(`COPY (SELECT * FROM zzz) TO 'non_existing.csv'`);
await conn.close();
await db.dropFile('test.csv');
await db.reset();
Expand Down

0 comments on commit 0f29e56

Please sign in to comment.