From 1f7247e3b617be386225f59ff330aaf68d39b346 Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Sun, 15 Nov 2026 02:45:52 +0100 Subject: [PATCH 1/5] fix: accept standalone server preferences (RUST-CI-043) --- crates/emulebb-rest/src/tests/route_app.rs | 145 +++++++++++++++++++++ crates/emulebb-settings/src/lib.rs | 33 +++++ 2 files changed, 178 insertions(+) diff --git a/crates/emulebb-rest/src/tests/route_app.rs b/crates/emulebb-rest/src/tests/route_app.rs index 32d815fb1..6299be78d 100644 --- a/crates/emulebb-rest/src/tests/route_app.rs +++ b/crates/emulebb-rest/src/tests/route_app.rs @@ -64,6 +64,151 @@ fn atomic_settings_rows(connection: &rusqlite::Connection) -> Vec<(String, Strin .unwrap() } +#[tokio::test] +async fn standalone_server_preferences_round_trip_through_sqlite_and_restart() { + for field in ["useServerPriorities", "autoConnectStaticOnly"] { + for value in [false, true] { + let (app, core, database, connection) = atomic_settings_router(); + let (status, before) = atomic_settings_response(&app, "GET", Value::Null).await; + assert_eq!(status, StatusCode::OK); + let runtime_core = core.core_settings().await; + let mut expected = before["data"].clone(); + expected["ed2k"][field] = json!(value); + let (status, patched) = + atomic_settings_response(&app, "PATCH", json!({"ed2k": {field: value}})).await; + assert_eq!(status, StatusCode::OK, "{field}={value}: {patched}"); + assert_eq!(patched["data"], expected); + let (status, fetched) = atomic_settings_response(&app, "GET", Value::Null).await; + assert_eq!(status, StatusCode::OK); + assert_eq!(fetched["data"], expected); + let persisted: String = connection + .query_row( + "SELECT value_json FROM settings WHERE section = 'ed2k' AND key = ?1", + [field], + |row| row.get(0), + ) + .unwrap(); + assert_eq!( + serde_json::from_str::(&persisted).unwrap(), + json!(value) + ); + assert_eq!(core.core_settings().await, runtime_core); + + let rows = atomic_settings_rows(&connection); + for empty in [json!({}), json!({"ed2k": {}})] { + let (status, response) = atomic_settings_response(&app, "PATCH", empty).await; + assert_eq!(status, StatusCode::BAD_REQUEST, "{response}"); + assert_eq!(response["error"]["code"], "INVALID_ARGUMENT"); + assert_eq!(atomic_settings_rows(&connection), rows); + } + + // Reopen only after the original owner is dropped: these settings + // remain durable configuration for the next daemon startup. + drop(app); + drop(core); + drop(connection); + let restored = EmulebbCore::new( + "test", + FileIndex::open(database).unwrap(), + unique_test_dir("server-preferences-restart"), + ) + .unwrap(); + assert_eq!( + serde_json::to_value(restored.app_settings().await.unwrap()).unwrap(), + expected, + ); + } + } +} + +#[tokio::test] +async fn invalid_nat_boolean_with_valid_siblings_preserves_all_settings() { + let (app, core, _, connection) = atomic_settings_router(); + let (_, before) = atomic_settings_response(&app, "GET", Value::Null).await; + let runtime_core = core.core_settings().await; + let rows = atomic_settings_rows(&connection); + for field in ["enabled", "requireInitialMapping"] { + let (status, rejected) = atomic_settings_response( + &app, + "PATCH", + json!({ + "core": {"uploadLimitKiBps": 1024}, + "ed2k": {"useServerPriorities": false}, + "nat": {field: null, "leaseDurationSecs": 7200}, + }), + ) + .await; + assert_eq!(status, StatusCode::BAD_REQUEST, "{rejected}"); + assert_eq!(rejected["error"]["code"], "INVALID_ARGUMENT"); + let (status, after) = atomic_settings_response(&app, "GET", Value::Null).await; + assert_eq!(status, StatusCode::OK); + assert_eq!(after["data"], before["data"]); + assert_eq!(atomic_settings_rows(&connection), rows); + assert_eq!(core.core_settings().await, runtime_core); + } +} + +#[tokio::test] +async fn nullable_nat_addresses_can_still_be_cleared_and_remain_cleared_after_restart() { + let (app, core, database, connection) = atomic_settings_router(); + let (_, before) = atomic_settings_response(&app, "GET", Value::Null).await; + let (status, seeded) = atomic_settings_response( + &app, + "PATCH", + json!({"nat": { + "bindIp": "192.0.2.10", "pcpServerIp": "192.0.2.1", + "igdIp": "192.0.2.2", "externalIpOverride": "198.51.100.24", + }}), + ) + .await; + assert_eq!(status, StatusCode::OK, "{seeded}"); + let fields = ["bindIp", "pcpServerIp", "igdIp", "externalIpOverride"]; + for field in fields { + assert!(seeded["data"]["nat"][field].is_string()); + } + let (status, cleared) = atomic_settings_response( + &app, + "PATCH", + json!({"nat": { + "bindIp": null, "pcpServerIp": null, + "igdIp": null, "externalIpOverride": null, + }}), + ) + .await; + assert_eq!(status, StatusCode::OK, "{cleared}"); + let mut expected = before["data"].clone(); + for field in fields { + expected["nat"][field] = Value::Null; + let persisted: String = connection + .query_row( + "SELECT value_json FROM settings WHERE section = 'nat' AND key = ?1", + [field], + |row| row.get(0), + ) + .unwrap(); + assert_eq!( + serde_json::from_str::(&persisted).unwrap(), + Value::Null + ); + } + assert_eq!(cleared["data"], expected); + let (_, fetched) = atomic_settings_response(&app, "GET", Value::Null).await; + assert_eq!(fetched["data"], expected); + drop(app); + drop(core); + drop(connection); + let restored = EmulebbCore::new( + "test", + FileIndex::open(database).unwrap(), + unique_test_dir("nullable-nat-restart"), + ) + .unwrap(); + assert_eq!( + serde_json::to_value(restored.app_settings().await.unwrap()).unwrap(), + expected, + ); +} + #[tokio::test] async fn settings_http_middleware_rejects_invalid_core_before_any_setting_changes() { let (app, core, _, connection) = atomic_settings_router(); diff --git a/crates/emulebb-settings/src/lib.rs b/crates/emulebb-settings/src/lib.rs index 33edb470c..35e67c2a7 100644 --- a/crates/emulebb-settings/src/lib.rs +++ b/crates/emulebb-settings/src/lib.rs @@ -1200,6 +1200,8 @@ impl Ed2kSettingsUpdate { && self.reconnect_interval_secs.is_none() && self.reconnect_enabled.is_none() && self.safe_server_connect.is_none() + && self.use_server_priorities.is_none() + && self.auto_connect_static_only.is_none() && self.server_keepalive_minutes.is_none() && self.session_rotation_secs.is_none() && self.max_concurrent_downloads.is_none() @@ -1989,6 +1991,37 @@ fn changed(next: T, current: T) -> Option { mod tests { use super::*; + #[test] + fn standalone_server_preferences_are_nonempty_and_preserve_omitted_fields() { + assert!(Ed2kSettingsUpdate::default().is_empty()); + assert!(app_settings_update_is_empty(&AppSettingsUpdate { + ed2k: Some(Ed2kSettingsUpdate::default()), + ..Default::default() + })); + for field in ["useServerPriorities", "autoConnectStaticOnly"] { + let path = format!("ed2k.{field}"); + assert!( + app_settings_surface_inventory() + .iter() + .any(|setting| setting.path == path && setting.restart_required) + ); + for value in [false, true] { + let update: Ed2kSettingsUpdate = + serde_json::from_value(serde_json::json!({field: value})).unwrap(); + assert!(!update.is_empty(), "{field}={value}"); + assert!(!app_settings_update_is_empty(&AppSettingsUpdate { + ed2k: Some(update.clone()), + ..Default::default() + })); + let mut settings = Ed2kSettings::default(); + let mut expected = serde_json::to_value(&settings).unwrap(); + expected[field] = serde_json::json!(value); + apply_ed2k_settings_update(&mut settings, update).unwrap(); + assert_eq!(serde_json::to_value(settings).unwrap(), expected); + } + } + } + #[test] fn default_core_settings_include_current_rest_fields() { let value = serde_json::to_value(default_core_settings()).unwrap(); From ac63c6097932b7dfb55a5b5ca0517e16cf2411d3 Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Sun, 15 Nov 2026 02:45:34 +0100 Subject: [PATCH 2/5] fix: reject null NAT boolean settings (RUST-CI-043) --- .../src/route_body_metadata/validators.rs | 3 + .../src/tests/route_body_validation.rs | 92 +++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/crates/emulebb-rest/src/route_body_metadata/validators.rs b/crates/emulebb-rest/src/route_body_metadata/validators.rs index 850143ff7..869c252a3 100644 --- a/crates/emulebb-rest/src/route_body_metadata/validators.rs +++ b/crates/emulebb-rest/src/route_body_metadata/validators.rs @@ -519,6 +519,9 @@ fn validate_kad_settings_patch_body_fields(object: &JsonObject) -> Result<(), Bo } fn validate_nat_settings_patch_body_fields(object: &JsonObject) -> Result<(), Box> { + // WHY: Reject explicit null before Option decoding can treat it as an omitted patch. + validate_optional_boolean_body_field(object, "enabled")?; + validate_optional_boolean_body_field(object, "requireInitialMapping")?; validate_nullable_ipv4_text(object, "bindIp", "settings.nat.bindIp")?; validate_nullable_ipv4_text(object, "pcpServerIp", "settings.nat.pcpServerIp")?; validate_nullable_ipv4_text(object, "igdIp", "settings.nat.igdIp")?; diff --git a/crates/emulebb-rest/src/tests/route_body_validation.rs b/crates/emulebb-rest/src/tests/route_body_validation.rs index 5b7c7558a..02b46d8e5 100644 --- a/crates/emulebb-rest/src/tests/route_body_validation.rs +++ b/crates/emulebb-rest/src/tests/route_body_validation.rs @@ -746,6 +746,98 @@ async fn core_settings_patch_body_uses_canonical_validation() { } } +#[tokio::test] +async fn nat_settings_patch_booleans_reject_non_boolean_values_with_valid_siblings() { + let app = test_router(); + for field in ["enabled", "requireInitialMapping"] { + for value in ["null", r#""true""#, "1", "[]", "{}"] { + for sibling in ["", r#", "renewMarginSecs":300"#] { + assert_invalid_json_response( + app.clone(), + "PATCH", + "/api/v1/app/settings", + format!(r#"{{"nat":{{"{field}":{value}{sibling}}}}}"#), + &format!("{field} must be a boolean"), + ) + .await; + } + } + } +} + +#[tokio::test] +async fn nat_settings_patch_booleans_accept_both_values_and_omission() { + for field in ["enabled", "requireInitialMapping"] { + let app = test_router(); + for value in [Some(false), Some(true), None] { + let mut nat = json!({"renewMarginSecs": 300}); + if let Some(value) = value { + nat[field] = json!(value); + } + let response = app + .clone() + .oneshot( + Request::builder() + .method("PATCH") + .uri("/api/v1/app/settings") + .header("X-API-Key", "secret") + .header("Content-Type", "application/json") + .body(Body::from(json!({"nat": nat}).to_string())) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::OK, "{field}: {value:?}"); + let body = to_bytes(response.into_body(), usize::MAX).await.unwrap(); + let response: Value = serde_json::from_slice(&body).unwrap(); + assert_eq!(response["data"]["nat"][field], value.unwrap_or(true)); + } + } +} + +#[tokio::test] +async fn nat_settings_patch_keeps_nullable_ip_and_socket_fields_clearable() { + let app = test_router(); + let fields = [ + "bindIp", + "pcpServerIp", + "igdIp", + "externalIpOverride", + "minissdpdSocket", + ]; + for clear in [false, true] { + let mut nat = json!({}); + for field in fields { + nat[field] = if clear { + Value::Null + } else if field == "minissdpdSocket" { + json!("/tmp/synthetic-minissdpd.sock") + } else { + json!("192.0.2.1") + }; + } + let response = app + .clone() + .oneshot( + Request::builder() + .method("PATCH") + .uri("/api/v1/app/settings") + .header("X-API-Key", "secret") + .header("Content-Type", "application/json") + .body(Body::from(json!({"nat": nat}).to_string())) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::OK); + let body = to_bytes(response.into_body(), usize::MAX).await.unwrap(); + let response: Value = serde_json::from_slice(&body).unwrap(); + for field in fields { + assert_eq!(response["data"]["nat"][field].is_null(), clear, "{field}"); + } + } +} + #[tokio::test] async fn transfer_add_body_keeps_canonical_link_validation_before_paused() { let app = test_router(); From 256ec5d38413ba57744053e99ecddf2fa351aadd Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Sun, 15 Nov 2026 02:41:08 +0100 Subject: [PATCH 3/5] docs: update Android release links (RUST-FEAT-056) --- README.md | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 637c9ce6b..48eedfd86 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@

- Use eMuleBB Rust  ·  + Use eMuleBB Rust  ·  Develop with eMuleBB Rust

@@ -58,34 +58,34 @@ channel. Prebuilt packages need no Rust toolchain. Choose a runnable asset from the [nightly releases](https://github.com/emulebb/emulebb-rust/releases), then compare its exact filename against that release's `SHA256SUMS`. -The [platform run guide](https://github.com/emulebb/emulebb-tooling/blob/f88a8ce608bc75a619aea6909f54a9cdecdddb6e/docs/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS.md) +The [platform run guide](https://emulebb.github.io/emulebb-tooling/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS/) owns installation commands, profile paths, requirements and qualification scope. | Platform | Choose | Install and run | | --- | --- | --- | -| Windows | x64 or ARM64 ZIP | [Extract and launch](https://github.com/emulebb/emulebb-tooling/blob/f88a8ce608bc75a619aea6909f54a9cdecdddb6e/docs/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS.md#windows-x64-and-arm64) | -| Linux | x64 or ARM64 DEB, AppImage or static-musl tar/zstd | [Choose a Linux variant](https://github.com/emulebb/emulebb-tooling/blob/f88a8ce608bc75a619aea6909f54a9cdecdddb6e/docs/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS.md#linux) | -| macOS | Intel or Apple Silicon DMG | [Copy and launch the app](https://github.com/emulebb/emulebb-tooling/blob/f88a8ce608bc75a619aea6909f54a9cdecdddb6e/docs/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS.md#macos-intel-and-apple-silicon) | -| Docker / OCI | Linux amd64 or arm64; Ubuntu or Alpine | [Run with persistent volumes](https://github.com/emulebb/emulebb-tooling/blob/f88a8ce608bc75a619aea6909f54a9cdecdddb6e/docs/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS.md#containers) | -| Android APK | Universal ARM64 + x86_64; Android 12+ | [Permissions, manual Start and WebUI](https://github.com/emulebb/emulebb-tooling/blob/f88a8ce608bc75a619aea6909f54a9cdecdddb6e/docs/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS.md#android-application) | -| Android / Termux native | Experimental `android-arm64-v8a.tar.zst` or `android-x86_64.tar.zst` | [Extract under private HOME and launch](https://github.com/emulebb/emulebb-tooling/blob/f88a8ce608bc75a619aea6909f54a9cdecdddb6e/docs/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS.md#termux-native-shell) | +| Windows | x64 or ARM64 ZIP | [Extract and launch](https://emulebb.github.io/emulebb-tooling/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS/#windows-x64-and-arm64) | +| Linux | x64 or ARM64 DEB, AppImage or static-musl tar/zstd | [Choose a Linux variant](https://emulebb.github.io/emulebb-tooling/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS/#linux) | +| macOS | Intel or Apple Silicon DMG | [Copy and launch the app](https://emulebb.github.io/emulebb-tooling/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS/#macos-intel-and-apple-silicon) | +| Docker / OCI | Linux amd64 or arm64; Ubuntu or Alpine | [Run with persistent volumes](https://emulebb.github.io/emulebb-tooling/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS/#containers) | +| Android APK | Universal ARM64 + x86_64; Android 12+ | [Permissions, manual Start and WebUI](https://emulebb.github.io/emulebb-tooling/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS/#android-application) | +| Android / Termux native | Experimental `android-arm64-v8a.tar.zst` or `android-x86_64.tar.zst` | [Extract under private HOME and launch](https://emulebb.github.io/emulebb-tooling/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS/#termux-native-shell) | The macOS app is unsigned and not notarized. For containers, both [`ghcr.io/emulebb/emulebb-rust`](https://github.com/emulebb/emulebb-rust/pkgs/container/emulebb-rust) variants publish versioned tags and moving `nightly` / `nightly-ubuntu` / `nightly-alpine` aliases; there is no `latest` tag. -**Android remains experimental.** Use the signed APK only when the selected -published release includes it after qualification. Both native ABI archives -remain separate public nightly assets alongside that APK. Their API 28 binary -floor and compile/ELF evidence do not qualify Termux runtime, the full Android -API matrix, Fire OS or 24-hour continuous operation. The APK minimum is Android -12/API 31; its foreground service starts manually and has no boot-resume option. -See [Android evidence and remaining acceptance](https://github.com/emulebb/emulebb-tooling/blob/f88a8ce608bc75a619aea6909f54a9cdecdddb6e/docs/products/emulebb-rust/active/items/RUST-FEAT-056.md). +**Android remains experimental.** The nightly release contract includes the +signed Android 12+ APK and both native archives, `android-arm64-v8a.tar.zst` +and `android-x86_64.tar.zst`. Follow the platform guide for installation and +manual foreground-service start, including after reboot. The native archives +target API 28 for compilation; compile and ELF checks do not qualify Termux or +Fire OS runtime. Termux runtime, Fire OS, automatic boot-resume and 24-hour +endurance remain unqualified. See [Android evidence and remaining acceptance](https://emulebb.github.io/emulebb-tooling/products/emulebb-rust/active/items/RUST-FEAT-056/). ## Get started -1. Follow your [platform's run instructions](https://github.com/emulebb/emulebb-tooling/blob/f88a8ce608bc75a619aea6909f54a9cdecdddb6e/docs/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS.md). +1. Follow your [platform's run instructions](https://emulebb.github.io/emulebb-tooling/products/emulebb-rust/reference/GUIDE-DOWNLOADS-PLATFORMS/). Keep the complete extracted archive layout, including `webui`. 2. Open **** on the daemon host. The default listener is **`0.0.0.0:20017`**. Native packages use the ready key @@ -103,7 +103,7 @@ settings; mutable client settings live in the profile database. Startup may fetch discovery inputs and start configured networking. During beta, an incompatible database requires an entirely fresh profile; preserve the stopped old profile, payloads and matching binary for rollback. The -[getting-started guide](https://github.com/emulebb/emulebb-tooling/blob/f88a8ce608bc75a619aea6909f54a9cdecdddb6e/docs/products/emulebb-rust/reference/GUIDE-GETTING-STARTED.md) +[getting-started guide](https://emulebb.github.io/emulebb-tooling/products/emulebb-rust/reference/GUIDE-GETTING-STARTED/) covers everyday operation, shutdown and troubleshooting. ## Use the API @@ -144,10 +144,10 @@ this trust set. Updated roots arrive with new client builds. ## Help and security -- [Troubleshoot startup, listeners and logs](https://github.com/emulebb/emulebb-tooling/blob/f88a8ce608bc75a619aea6909f54a9cdecdddb6e/docs/products/emulebb-rust/reference/GUIDE-GETTING-STARTED.md#common-problems). +- [Troubleshoot startup, listeners and logs](https://emulebb.github.io/emulebb-tooling/products/emulebb-rust/reference/GUIDE-GETTING-STARTED/#common-problems). - [Report a bug](https://github.com/emulebb/emulebb-rust/issues) with the package version and sanitized reproduction steps. -- Follow [private security reporting](SECURITY.md) for vulnerabilities. +- Follow [private security reporting](SECURITY/) for vulnerabilities. Diagnostic bundles are private operator evidence and can contain credentials, paths, identifiers and logs. Review them before sharing publicly. @@ -155,4 +155,4 @@ paths, identifiers and logs. Review them before sharing publicly. ## License [GPL-2.0-only](LICENSE). Dependencies retain their own licenses; see -[third-party notices](THIRD-PARTY-LICENSES.md). +[third-party notices](THIRD-PARTY-LICENSES/). From 42850fe9120d55ad4f7321da834d88cba38b42dc Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Sun, 15 Nov 2026 02:45:41 +0100 Subject: [PATCH 4/5] docs: fix guide and repository links (RUST-FEAT-056) --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 48eedfd86..73b46c9ac 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@

- Use eMuleBB Rust  ·  + Use eMuleBB Rust  ·  Develop with eMuleBB Rust

@@ -147,7 +147,7 @@ this trust set. Updated roots arrive with new client builds. - [Troubleshoot startup, listeners and logs](https://emulebb.github.io/emulebb-tooling/products/emulebb-rust/reference/GUIDE-GETTING-STARTED/#common-problems). - [Report a bug](https://github.com/emulebb/emulebb-rust/issues) with the package version and sanitized reproduction steps. -- Follow [private security reporting](SECURITY/) for vulnerabilities. +- Follow [private security reporting](SECURITY.md) for vulnerabilities. Diagnostic bundles are private operator evidence and can contain credentials, paths, identifiers and logs. Review them before sharing publicly. @@ -155,4 +155,4 @@ paths, identifiers and logs. Review them before sharing publicly. ## License [GPL-2.0-only](LICENSE). Dependencies retain their own licenses; see -[third-party notices](THIRD-PARTY-LICENSES/). +[third-party notices](THIRD-PARTY-LICENSES.md). From d6e44b1cec8e8110011ff1f988bce8869a90e7dc Mon Sep 17 00:00:00 2001 From: Threepwood-7 Date: Sun, 15 Nov 2026 04:01:05 +0100 Subject: [PATCH 5/5] fix: release profile leases with their logical owner (RUST-CI-043) --- Cargo.lock | 1 + crates/emulebb-daemon/src/lib.rs | 6 +- .../emulebb-daemon/src/profile_ownership.rs | 19 ++- .../emulebb-daemon/tests/profile_ownership.rs | 15 ++ crates/emulebb-metadata/Cargo.toml | 3 + crates/emulebb-metadata/src/lib.rs | 2 + .../emulebb-metadata/src/profile_ownership.rs | 39 +++++ crates/emulebb-metadata/src/store.rs | 13 +- .../tests/profile_ownership.rs | 159 ++++++++++++++++++ 9 files changed, 240 insertions(+), 17 deletions(-) create mode 100644 crates/emulebb-metadata/src/profile_ownership.rs create mode 100644 crates/emulebb-metadata/tests/profile_ownership.rs diff --git a/Cargo.lock b/Cargo.lock index 9ab5fca93..7f900e0a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -796,6 +796,7 @@ dependencies = [ "anyhow", "data-encoding", "hex", + "libc", "rusqlite", "unicode-normalization", "uuid", diff --git a/crates/emulebb-daemon/src/lib.rs b/crates/emulebb-daemon/src/lib.rs index 5bdca50bd..22f57397c 100644 --- a/crates/emulebb-daemon/src/lib.rs +++ b/crates/emulebb-daemon/src/lib.rs @@ -191,7 +191,7 @@ impl DaemonProfile { let opened = match host_folders { Some((incoming, shared)) => MetadataStore::open_with_profile_lease_and_storage( metadata_path, - profile_lease.file(), + profile_lease.guard(), move || { anyhow::ensure!( incoming.is_absolute() && shared.is_absolute(), @@ -209,7 +209,7 @@ impl DaemonProfile { }) }, ), - None => MetadataStore::open_with_profile_lease(metadata_path, profile_lease.file()), + None => MetadataStore::open_with_profile_lease(metadata_path, profile_lease.guard()), }; let metadata = opened.with_context(|| { format!( @@ -267,7 +267,7 @@ impl DaemonProfile { .context("load the profile before opening writable metadata")?; MetadataStore::open_with_profile_lease( lease.directory().join(PROFILE_METADATA_FILE), - lease.file(), + lease.guard(), ) } diff --git a/crates/emulebb-daemon/src/profile_ownership.rs b/crates/emulebb-daemon/src/profile_ownership.rs index 047ad910a..749f21d6c 100644 --- a/crates/emulebb-daemon/src/profile_ownership.rs +++ b/crates/emulebb-daemon/src/profile_ownership.rs @@ -1,25 +1,26 @@ //! OS-backed ownership of a writable profile, retained by all metadata handles. use std::{ - fs::{self, File, OpenOptions, TryLockError}, + fs::{self, OpenOptions, TryLockError}, path::{Path, PathBuf}, sync::Arc, }; use anyhow::{Context, Result, bail, ensure}; +use emulebb_metadata::ProfileLeaseGuard; const PROFILE_LEASE_FILE: &str = ".emulebb-profile.lock"; /// An opaque, cloneable lease for one physical profile directory. /// /// The persistent empty file is a lock target, not an ownership marker. Never -/// remove it on release: the operating system releases ownership when the last -/// handle closes, including after process death. Locks must be supported by the -/// selected filesystem; an unsupported/failed lock is an error, never a bypass. +/// remove it on release: the final logical owner unlocks its acquired descriptor, +/// and the operating system releases ownership after process death. Locks must +/// be supported by the selected filesystem; a failed lock is never a bypass. #[derive(Debug, Clone)] pub struct ProfileLease { directory: PathBuf, - file: Arc, + guard: Arc, } impl ProfileLease { @@ -63,8 +64,8 @@ impl ProfileLease { profile.display() ) })?; - match file.try_lock() { - Ok(()) => Ok(Self { directory: canonical_profile, file: Arc::new(file) }), + match ProfileLeaseGuard::try_lock(file) { + Ok(guard) => Ok(Self { directory: canonical_profile, guard: Arc::new(guard) }), Err(TryLockError::WouldBlock) => bail!( "profile is already in use: {}; stop its daemon or offline command before retrying", profile.display() @@ -82,8 +83,8 @@ impl ProfileLease { &self.directory } - pub(crate) fn file(&self) -> Arc { - Arc::clone(&self.file) + pub(crate) fn guard(&self) -> Arc { + Arc::clone(&self.guard) } } diff --git a/crates/emulebb-daemon/tests/profile_ownership.rs b/crates/emulebb-daemon/tests/profile_ownership.rs index e777a547d..c3429257b 100644 --- a/crates/emulebb-daemon/tests/profile_ownership.rs +++ b/crates/emulebb-daemon/tests/profile_ownership.rs @@ -240,6 +240,21 @@ fn unrelated_profiles_can_be_owned_concurrently() { second.finish(); } +#[test] +fn profile_clones_and_metadata_handles_share_one_logical_lease() { + let temporary = temporary(); + let path = temporary.path().join("profile"); + let profile = DaemonProfile::load_for_import(path.clone()).unwrap(); + let cloned = profile.clone(); + let metadata = profile.open_metadata_store().unwrap(); + drop(profile); + probe(&path, "busy", &temporary.path().join("profile-clone")); + drop(cloned); + probe(&path, "busy", &temporary.path().join("metadata")); + drop(metadata); + probe(&path, "available", &temporary.path().join("released")); +} + #[test] #[ignore = "isolated subprocess fixture invoked by the ownership regression tests"] fn profile_ownership_child() { diff --git a/crates/emulebb-metadata/Cargo.toml b/crates/emulebb-metadata/Cargo.toml index fdf7dabb1..d1b3a4bc4 100644 --- a/crates/emulebb-metadata/Cargo.toml +++ b/crates/emulebb-metadata/Cargo.toml @@ -17,3 +17,6 @@ hex.workspace = true rusqlite.workspace = true unicode-normalization.workspace = true uuid.workspace = true + +[target.'cfg(unix)'.dev-dependencies] +libc.workspace = true diff --git a/crates/emulebb-metadata/src/lib.rs b/crates/emulebb-metadata/src/lib.rs index ba962934f..adf63e9eb 100644 --- a/crates/emulebb-metadata/src/lib.rs +++ b/crates/emulebb-metadata/src/lib.rs @@ -19,6 +19,7 @@ mod peer_model; mod peer_store; mod profile_import; mod profile_model; +mod profile_ownership; mod profile_store; mod schema; mod schema_validation; @@ -51,6 +52,7 @@ pub use native_file::{NativeFileFingerprint, NativeFileIdentity, NativeFileTimes pub use native_path::NativePath; pub use peer_model::MetadataPeerCredit; pub use profile_model::{MetadataCategory, MetadataFriend, MetadataServer}; +pub use profile_ownership::ProfileLeaseGuard; pub use schema::{SCHEMA_ID, SCHEMA_SQL, SCHEMA_VERSION}; pub use search_model::{ MetadataSearch, MetadataSearchResult, MetadataSearchResultObservation, MetadataSearchSpec, diff --git a/crates/emulebb-metadata/src/profile_ownership.rs b/crates/emulebb-metadata/src/profile_ownership.rs new file mode 100644 index 000000000..d8228d52a --- /dev/null +++ b/crates/emulebb-metadata/src/profile_ownership.rs @@ -0,0 +1,39 @@ +//! Descriptor ownership shared by profile loaders and their metadata handles. + +use std::fs::{File, TryLockError}; + +/// An acquired exclusive profile lock, shared through `Arc` by logical owners. +/// +/// Release never removes the persistent lock file. The acquiring process +/// explicitly unlocks its descriptor when the final owner drops, so transient +/// descriptors inherited by a spawning child cannot prolong profile ownership. +#[derive(Debug)] +pub struct ProfileLeaseGuard { + file: File, + acquisition_pid: u32, +} + +impl ProfileLeaseGuard { + /// Acquire the supplied lock-file descriptor without waiting. + /// + /// Contention, unsupported locks, and other I/O failures remain errors. + /// Only a successfully acquired descriptor becomes an owned guard. + pub fn try_lock(file: File) -> Result { + file.try_lock()?; + Ok(Self { + file, + acquisition_pid: std::process::id(), + }) + } +} + +impl Drop for ProfileLeaseGuard { + fn drop(&mut self) { + // WHY: a fork/spawn can temporarily retain the same open-file description + // after the final logical owner drops. Unlock that acquired descriptor + // explicitly, but never let an inherited child unlock its parent's lease. + if self.acquisition_pid == std::process::id() { + let _ = self.file.unlock(); + } + } +} diff --git a/crates/emulebb-metadata/src/store.rs b/crates/emulebb-metadata/src/store.rs index 18b921298..d2d70fb7d 100644 --- a/crates/emulebb-metadata/src/store.rs +++ b/crates/emulebb-metadata/src/store.rs @@ -1,5 +1,4 @@ use std::{ - fs::File, path::{Path, PathBuf}, sync::{Arc, Mutex, MutexGuard, TryLockError}, time::{SystemTime, UNIX_EPOCH}, @@ -10,6 +9,7 @@ use rusqlite::{Connection, OptionalExtension, params}; use uuid::Uuid; use crate::{ + ProfileLeaseGuard, model::{MetadataIndexedFile, MetadataIndexedFileOrigin, MetadataSharedDirectoryRoot}, native_path::{NativePath, current_platform}, schema::{SCHEMA_ID, SCHEMA_SQL, SCHEMA_VERSION}, @@ -34,7 +34,7 @@ pub struct MetadataStore { conn: Arc>, // WHY: close the connection before releasing its process profile lease. // Clones retain both handles; independent profile connections share the lease. - _profile_lease: Option>, + _profile_lease: Option>, initialized_new_schema: bool, } @@ -48,7 +48,10 @@ impl MetadataStore { /// /// The caller acquires the lock before bootstrap/metadata initialization. /// Every cloned store retains it until its connection has finished closing. - pub fn open_with_profile_lease(path: impl AsRef, lease: Arc) -> Result { + pub fn open_with_profile_lease( + path: impl AsRef, + lease: Arc, + ) -> Result { let conn = Connection::open(path)?; Self::from_connection_with_profile_lease(conn, Some(lease), None) } @@ -57,7 +60,7 @@ impl MetadataStore { /// never called for an existing schema, including an empty operator root list. pub fn open_with_profile_lease_and_storage( path: impl AsRef, - lease: Arc, + lease: Arc, initialize: impl FnOnce() -> Result + 'static, ) -> Result { Self::from_connection_with_profile_lease( @@ -82,7 +85,7 @@ impl MetadataStore { fn from_connection_with_profile_lease( conn: Connection, - profile_lease: Option>, + profile_lease: Option>, initialize_storage: Option, ) -> Result { // The guarded constructor retains ownership before any pragma/schema diff --git a/crates/emulebb-metadata/tests/profile_ownership.rs b/crates/emulebb-metadata/tests/profile_ownership.rs new file mode 100644 index 000000000..6a3ab7234 --- /dev/null +++ b/crates/emulebb-metadata/tests/profile_ownership.rs @@ -0,0 +1,159 @@ +//! Logical profile ownership and inherited-descriptor lifecycle regressions. + +use std::{ + fs::{self, File, OpenOptions, TryLockError}, + path::PathBuf, + sync::Arc, +}; + +use emulebb_metadata::{MetadataStore, ProfileLeaseGuard}; + +struct Fixture(PathBuf); + +impl Fixture { + fn new() -> Self { + let target = PathBuf::from( + std::env::var_os("CARGO_TARGET_DIR").expect("ownership tests require CARGO_TARGET_DIR"), + ); + assert!(target.is_absolute()); + let directory = target + .join("tmp/metadata-profile-ownership") + .join(uuid::Uuid::new_v4().to_string()); + fs::create_dir_all(&directory).unwrap(); + Self(directory) + } + + fn file(&self) -> File { + OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(false) + .open(self.0.join(".emulebb-profile.lock")) + .unwrap() + } + + fn acquire(&self) -> ProfileLeaseGuard { + ProfileLeaseGuard::try_lock(self.file()).unwrap() + } + + fn assert_busy(&self) { + assert!(matches!( + ProfileLeaseGuard::try_lock(self.file()), + Err(TryLockError::WouldBlock) + )); + } +} + +impl Drop for Fixture { + fn drop(&mut self) { + fs::remove_dir_all(&self.0).unwrap(); + } +} + +#[test] +fn final_logical_owner_unlocks_while_a_duplicate_descriptor_remains_open() { + let fixture = Fixture::new(); + let file = fixture.file(); + let duplicate = file.try_clone().unwrap(); + let guard = Arc::new(ProfileLeaseGuard::try_lock(file).unwrap()); + let cloned = Arc::clone(&guard); + fixture.assert_busy(); + drop(guard); + fixture.assert_busy(); + drop(cloned); + let next_owner = fixture.acquire(); + // The duplicate models a transient fork/spawn descriptor: it cannot retain + // ownership after the final logical owner explicitly releases the lease. + drop(duplicate); + fixture.assert_busy(); + drop(next_owner); + assert!(fixture.0.join(".emulebb-profile.lock").is_file()); +} + +#[test] +fn cloned_and_independent_metadata_handles_retain_the_lease() { + let fixture = Fixture::new(); + let file = fixture.file(); + let duplicate = file.try_clone().unwrap(); + let guard = Arc::new(ProfileLeaseGuard::try_lock(file).unwrap()); + let database = fixture.0.join("metadata.db"); + let first = MetadataStore::open_with_profile_lease(&database, Arc::clone(&guard)).unwrap(); + let cloned = first.clone(); + let independent = + MetadataStore::open_with_profile_lease(&database, Arc::clone(&guard)).unwrap(); + drop(guard); + drop(first); + fixture.assert_busy(); + drop(cloned); + fixture.assert_busy(); + drop(independent); + let next_owner = fixture.acquire(); + // Closing the final connection checkpoints its WAL before ownership passes. + assert!(!database.with_extension("db-wal").exists()); + assert!(!database.with_extension("db-shm").exists()); + drop(duplicate); + drop(next_owner); +} + +#[test] +fn metadata_constructor_errors_release_the_final_logical_owner() { + let fixture = Fixture::new(); + let file = fixture.file(); + let duplicate = file.try_clone().unwrap(); + let guard = Arc::new(ProfileLeaseGuard::try_lock(file).unwrap()); + let database = fixture.0.join("metadata.db"); + let result = MetadataStore::open_with_profile_lease_and_storage(&database, guard, || { + anyhow::bail!("injected storage initialization failure") + }); + assert!(result.is_err()); + let file = fixture.file(); + let open_error_duplicate = file.try_clone().unwrap(); + let guard = Arc::new(ProfileLeaseGuard::try_lock(file).unwrap()); + assert!(MetadataStore::open_with_profile_lease(&fixture.0, guard).is_err()); + let next_owner = fixture.acquire(); + drop(duplicate); + drop(open_error_duplicate); + drop(next_owner); +} + +#[cfg(unix)] +#[test] +fn inherited_child_drop_cannot_unlock_the_parent_lease() { + let fixture = Fixture::new(); + let guard = fixture.acquire(); + // SAFETY: the fork child touches only this stack-owned File+PID guard, then + // calls _exit. Guard Drop checks the PID via getpid (std::process::id), skips + // unlock in the child, and closes File. getpid, close and _exit are + // async-signal-safe; no Arc, allocation, mutex, logging, assertion, fixture + // teardown or test-harness return occurs in the child of this threaded test. + let child = unsafe { libc::fork() }; + if child == 0 { + drop(guard); + // SAFETY: exit without running inherited Rust or test-harness destructors. + unsafe { libc::_exit(0) }; + } + assert!( + child > 0, + "fork failed: {}", + std::io::Error::last_os_error() + ); + let mut status = 0; + loop { + // SAFETY: child is our positive fork PID and status is writable storage. + let waited = unsafe { libc::waitpid(child, &mut status, 0) }; + if waited == child { + break; + } + assert_eq!(waited, -1); + assert_eq!( + std::io::Error::last_os_error().kind(), + std::io::ErrorKind::Interrupted + ); + } + assert!(libc::WIFEXITED(status), "child status: {status}"); + assert_eq!(libc::WEXITSTATUS(status), 0); + fixture.assert_busy(); + drop(guard); + drop(fixture.acquire()); +}