Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions native/src/main/java/org/humemai/nativelib/ArcadeNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ public static CCharPointer close(final IsolateThread thread, final long h, final
NESTED_BLOCKS.get().keySet().removeIf(key -> key >>> 32 == h);
return OK();
} catch (final Throwable t) {
// engine 26.12.1: drop() closes the database before it throws that a file could not be deleted; forget the closed handle
final Database closed = drop != 0 ? DBS.get(h) : null;
if (closed != null && !closed.isOpen()) {
DBS.remove(h);
NESTED_BLOCKS.get().keySet().removeIf(key -> key >>> 32 == h);
}
return err(t);
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/arcadedb/_native/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,18 @@ def _close_async_executors(self):
def drop(self):
self._check_not_closed()
lib.flush_closes()
_call("Failed to drop database", "arcadedb_n_close", self._h, 1)
try:
_call("Failed to drop database", "arcadedb_n_close", self._h, 1)
except ArcadeDBError:
# engine 26.12.1: drop() closes the database, deletes the files and then throws when one stays (#554); the library
# forgets a handle whose database is closed, so a handle it still knows is a drop that failed before the close
try:
still_open = self._info()["open"]
except ArcadeDBError:
still_open = False
if not still_open:
self._closed = True
raise
self._closed = True

def __del__(self):
Expand Down
8 changes: 7 additions & 1 deletion src/arcadedb/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2175,9 +2175,15 @@ def drop(self) -> None:
self._check_not_closed()
try:
self._java_db.drop()
self._closed = True
except Exception as e:
# engine 26.12.1: drop() closes the database, deletes the files and then throws when one stays (#554)
try:
if not self._java_db.isOpen():
self._closed = True
except Exception: # nosec B110 - the error of the drop is the one to report
pass
raise ArcadeDBError(f"Failed to drop database: {e}") from e
self._closed = True

def is_transaction_active(self) -> bool:
"""
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@
"test_parity_gate_awk.py": "runs the workflows' awk step code",
# runs the bash code of the verify step of release-native.yml on small passed/ and wheels/ directories (#349)
"test_release_native_verify.py": "runs the bash code of a workflow step",
# makes a drop fail with chmod 0o500 on the database directory, which does not stop a delete on Windows (#554)
"test_drop_failure.py": "chmod cannot make a directory undeletable",
}
if sys.platform == "win32":
collect_ignore.extend(WINDOWS_NOT_APPLICABLE)
if hasattr(os, "geteuid") and os.geteuid() == 0:
# a directory made undeletable with chmod still lets root delete its files
collect_ignore.append("test_drop_failure.py")
if BACKEND != "native":
# the old JSON-row route that the native column frames are compared with (the module lives in the native package)
collect_ignore.append("test_native_column_frames_reference.py")
Expand Down
62 changes: 62 additions & 0 deletions tests/test_drop_failure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""A drop() that raises because a file stayed leaves a closed handle (#554).

Since engine 26.12.1 (ArcadeData/arcadedb#13609) `LocalDatabase.drop()` closes the database, deletes the files, and then throws
when an entry is left. The handle then says what the engine says: closed, and a later close() does nothing.
"""

import os
import stat

import pytest

import arcadedb
from arcadedb.exceptions import ArcadeDBError

SINCE = (26, 12, 1)


def _engine_version(db):
raw = str(db.query("sql", "SELECT version() AS v").first().get("v"))
parts = [
int(p) if p.isdigit() else 0
for p in raw.split()[0].split("-")[0].split(".")[:3]
]
return tuple(parts + [0] * (3 - len(parts)))


def test_a_drop_that_leaves_a_file_closes_the_handle(tmp_path):
path = tmp_path / "drop_fails"
db = arcadedb.create_database(str(path))
raises = (
_engine_version(db) >= SINCE
) # the engine before 26.12.1 logs the files it could not delete and returns
db.command("sql", "CREATE DOCUMENT TYPE Note")
old_mode = stat.S_IMODE(os.stat(path).st_mode)
os.chmod(path, 0o500) # no entry of the directory can be removed
try:
if raises:
with pytest.raises(ArcadeDBError):
db.drop()
else:
try:
db.drop()
except ArcadeDBError:
pass
finally:
os.chmod(path, old_mode)
assert db.is_open() is False
with pytest.raises(ArcadeDBError, match="Database is closed"):
db.get_name() # the handle is closed, as it is after close()
db.close() # does nothing
db.close()
if raises:
reopened = arcadedb.open_database(
str(path)
) # the files stayed, and the path is free to open again
try:
assert (
reopened.query("sql", "SELECT count(*) AS c FROM Note").first().get("c")
== 0
)
finally:
reopened.drop()
Loading