From 2daecbb4011552c4585b9e0dc8167cb576ce3464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=88=E7=BA=BE?= Date: Thu, 26 Nov 2026 09:34:22 +0800 Subject: [PATCH 1/2] feat: settle the enable gate from the carrier's own enable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The carrier enables after the children it loaded, so a component that only watched a poll timer attached too late: the children's OnEnable had already returned and read every capability as unavailable. The gate now subscribes to CarrierEnableSignal, which the carrier raises from its onEnable on the server thread, and runs its callback there directly. No thread check on that path: isPrimaryThread() answers false during EndstoneServer::init because the server thread id is not recorded yet, and the work would be queued onto a tick that runs after every child OnEnable. Polling stays as the timeout and as a fallback. It must also run synchronously when the signal has already been raised — the timer ticks every 50ms and Endstone sets the enabled flag before calling onEnable, so it can steal the claim just ahead of the signal and hand the work to that same tick. That cost one run in ten before the fix; 50 consecutive runs are green after it. --- .../PluginEnableGate.cs | 115 +++++++++++++++++- 1 file changed, 110 insertions(+), 5 deletions(-) diff --git a/src/Hosihikari.Carrier.Endstone/PluginEnableGate.cs b/src/Hosihikari.Carrier.Endstone/PluginEnableGate.cs index 153c812..07b0567 100644 --- a/src/Hosihikari.Carrier.Endstone/PluginEnableGate.cs +++ b/src/Hosihikari.Carrier.Endstone/PluginEnableGate.cs @@ -1,3 +1,4 @@ +using Hosihikari; using Hosihikari.Binding.Endstone; using Microsoft.Extensions.Logging; @@ -8,11 +9,12 @@ namespace Hosihikari.Carrier.Endstone; /// Carrier components start during the carrier's onLoad, but Endstone rejects both /// listener registration and scheduler submissions until the plugin is enabled — neither call /// fails loudly, so a component that touched them early would end up "started" with nothing -/// attached. This gate polls the enable flag and then hops to the server thread through the -/// scheduler, which is accepted from that point on. +/// attached. /// -/// Only the enable flag is read from the polling thread; every call that touches carrier state -/// happens inside the callback, on the server thread. +/// The enable is normally observed through , which the carrier +/// raises from its own onEnable on the server thread; then runs the +/// callback right there, during enable. The poll timer is only a fallback (and the timeout), for +/// the case where the signal never arrives. /// /// public sealed class PluginEnableGate : IDisposable @@ -53,12 +55,76 @@ private PluginEnableGate(Plugin plugin, Action onEnabled, ILogger? logger, TimeS return null; } - var gate = new PluginEnableGate(plugin, onEnabled, logger, timeout ?? TimeSpan.FromSeconds(30)); + // Generous on purpose: the wait spans the carrier's whole startup. A server with a large + // world loads for well over a minute before plugins are enabled, and giving up early + // leaves the capability dead for the rest of the session — far worse than a late start. + var gate = new PluginEnableGate(plugin, onEnabled, logger, timeout ?? TimeSpan.FromMinutes(5)); + gate._timer = new Timer(static state => ((PluginEnableGate)state!).Poll(), gate, PollInterval, PollInterval); + CarrierEnableSignal.Subscribe(gate.OnCarrierEnabled); + + // The signal may have been raised between the isEnabled check above and this + // subscription, which would leave the gate waiting for an event already past. Take the + // same path the signal takes, not Poll: this lands inside the enable, where the thread + // check in Poll is known to answer wrong. + if (CarrierEnableSignal.HasRaised) + { + gate.RunNow(); + } + return gate; } + /// The carrier announced its own enable; settle immediately. + /// + /// Runs on whatever thread raised the signal, which is the carrier's onEnable — the + /// one instant the enabled flag is already set. The thread check is deliberately skipped + /// here: during EndstoneServer::init the server thread id is not recorded yet, so + /// isPrimaryThread() answers false and the callback would be queued onto a tick that + /// runs only after every child OnEnable has already returned. + /// + private void OnCarrierEnabled() => RunNow(); + + /// Settles the gate on the calling thread, without a thread check. + /// + /// Only correct when the caller already knows it is on the server thread — the signal, and + /// the late-subscription backstop in . The check is skipped + /// because isPrimaryThread() answers false during EndstoneServer::init: the + /// server thread id is not recorded yet, so it falls through to comparing mainThread() + /// (server.cpp:693-705), and the callback would be queued onto a tick that runs only after + /// every child OnEnable has already returned. + /// + private void RunNow() + { + if (Interlocked.Exchange(ref _claimed, 1) != 0) + { + return; + } + + Interlocked.Exchange(ref _timer, null)?.Dispose(); + + if (_disposed) + { + return; + } + + try + { + _onEnabled(); + } + catch (Exception exception) + { + _logger?.LogError(exception, "The deferred start threw on the carrier enable."); + } + } + + /// The poll path: the timeout, and a timer that beat the signal. + /// + /// Reached from a timer thread. Its claim has to be released again by running synchronously + /// whenever the signal has already been raised — see the branch below for why handing that + /// case to the scheduler loses the race. + /// private void Poll() { if (_disposed) @@ -92,6 +158,29 @@ private void Poll() return; } + // Run it here when this really is the server thread, and also whenever the signal has + // already been raised: in that case the carrier is genuinely enabled, and the timer can + // otherwise steal the claim from the signal just before it fires — it would then hand the + // work to a tick that runs after every child OnEnable has already returned. The thread + // check cannot be relied on to tell those apart: during EndstoneServer::init it answers + // false even on the server thread (server.cpp:693-705). + if (IsServerThread() || CarrierEnableSignal.HasRaised) + { + try + { + if (!_disposed) + { + _onEnabled(); + } + } + catch (Exception exception) + { + _logger?.LogError(exception, "The deferred start threw on the server thread."); + } + + return; + } + try { // The carrier accepts scheduler work now that the plugin is enabled; the callback @@ -110,10 +199,26 @@ private void Poll() } } + private bool IsServerThread() + { + try + { + return _plugin.Interop.getServer()?.isPrimaryThread() ?? false; + } + catch + { + // A probe failure must not lose the start; falling back to the scheduler is correct + // on every thread, only slower. + return false; + } + } + /// public void Dispose() { _disposed = true; + + CarrierEnableSignal.Unsubscribe(OnCarrierEnabled); Interlocked.Exchange(ref _timer, null)?.Dispose(); } } From eaf2075b545452b2f27ea96070807dd0f19692b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=88=E7=BA=BE?= Date: Thu, 26 Nov 2026 09:41:00 +0800 Subject: [PATCH 2/2] ci: pin the abstractions at the carrier enable signal The gate subscribes to CarrierEnableSignal, which only exists from 59461a7 on. The previous pin predates it and the job would fail to resolve the type. The commit is not on the default branch yet, so the pin names it in full; actions/checkout resolves a full SHA from any branch. --- .github/workflows/verify.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index 3b46309..53feacd 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -46,7 +46,7 @@ jobs: with: repository: HosihikariFramework/PluginAbstractions path: Plugin.Abstractions - ref: 2e2c8fc1d794bb3ba78f73b3f90fbe338f5a0a30 + ref: 59461a7d261050b82c43f009b062651050503984 - uses: actions/setup-dotnet@v4 with: