diff --git a/native/src/main/java/org/humemai/nativelib/ArcadeNative.java b/native/src/main/java/org/humemai/nativelib/ArcadeNative.java index 585b6d81..c5019ccd 100644 --- a/native/src/main/java/org/humemai/nativelib/ArcadeNative.java +++ b/native/src/main/java/org/humemai/nativelib/ArcadeNative.java @@ -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); } } diff --git a/src/arcadedb/_native/database.py b/src/arcadedb/_native/database.py index ab6b5409..85cfe5c9 100644 --- a/src/arcadedb/_native/database.py +++ b/src/arcadedb/_native/database.py @@ -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): diff --git a/src/arcadedb/core.py b/src/arcadedb/core.py index f6b81415..5b8079a5 100644 --- a/src/arcadedb/core.py +++ b/src/arcadedb/core.py @@ -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: """ diff --git a/tests/conftest.py b/tests/conftest.py index 1a5f35d3..9a9ad8b9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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") diff --git a/tests/test_drop_failure.py b/tests/test_drop_failure.py new file mode 100644 index 00000000..15501ef2 --- /dev/null +++ b/tests/test_drop_failure.py @@ -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()