Repository navigation
Fix cross-instance system dialer state corruption - #6483
eliotcougar wants to merge 3 commits into
Conversation
The system dialer kept its DNS client and outbound manager in package globals. Starting a temporary core for an outbound delay or policy-group test replaced those globals, and closing that core could leave the live VPN resolving through a closed DNS client or routing through the wrong outbound manager. Attach lazy dependency providers to each core context and use them in DNS resolution and freedom dialing. Retain InitSystemDialer as a locked compatibility fallback for callers without an instance context. Add a regression test proving that closing a test instance does not corrupt lookups performed by the main instance.
Expose instance-scoped observatory update subscriptions so embedders can react when regular or burst probe results change without polling. Report the effective initial probe-cycle deadline, allowing temporary routing overrides to remain active long enough for a fresh result. Clone regular observation snapshots under lock and emit notifications only after result locks are released. Add coverage for subscription lifecycle, regular and burst updates, and configured probe deadlines.
Restore process-wide fallback initialization during ordinary core construction so external callers of LookupForIP retain the behavior available before dialer dependencies became instance-scoped. Internal resolution and dialer-proxy paths continue to use instance contexts. Add a regression test covering fallback initialization through core.New.
这不是设计考虑
Line 23 in 50231ea |
|
Thank you for pointing to that comment. I understand it as expressing a design intention that no more than one However, the situation addressed by this PR is not hypothetical behavior introduced by the PR. It is how the current v2rayNG and AndroidLibXrayLite code already operate. In current upstream v2rayNG:
Therefore, current v2rayNG can run one or more short-lived Xray currently permits this at runtime: If multiple instances in one process are categorically unsupported, I will try to implement an alternative solution. However, other implementations can also act like this. Leaving the current API and downstream implementation in place means this corruption remains possible. The patch preserves ordinary single-instance behavior unchanged. Would you be willing to reopen the PR for this technical discussion? |
|
I have reduced the scope of this PR to the cross-instance system-dialer state-corruption fix only. All Observatory-related changes have been removed from the branch. The broader Observatory and one-shot outbound-probing work required for proper implementation of bulk probing without spawning separate per-outbound processes is still ongoing and will be presented as a separate PR. If appropriate, please reopen this PR for review in its reduced form. |
Summary
This PR fixes process-global system-dialer state that can be corrupted when an embedding application creates multiple Xray core instances in the same process.
The scope has been deliberately reduced from the original version of this PR. All Observatory update-notification and probe-deadline changes have been removed. The ongoing Observatory work, including APIs intended for outbound probing by third-party embedders, will be presented separately.
This PR now addresses only the cross-instance DNS client and outbound-manager corruption.
Problem
Some embedding applications currently create more than one
core.Instancein the same process.For example, v2rayNG may have:
Before this change, construction of every core called
InitSystemDialer, replacing package-level references to the DNS client and outbound manager.A temporary test core could therefore overwrite the system-dialer dependencies belonging to the live core. After that temporary core was closed, the live core could continue trying to resolve domains through a closed DNS client or use an outbound manager belonging to the wrong instance.
This can produce partial and inconsistent failures:
Root cause
The system dialer stored its DNS client and outbound manager in process-global variables even though those dependencies belong to a particular
core.Instance.Internal operations using those globals included:
dialerProxyoutbound-manager lookup;Creating another core replaced the globals for every instance in the process.
Changes
LookupForIPWithContextfor instance-aware DNS resolution.dialerProxythrough the outbound manager associated with the current context.InitSystemDialerandLookupForIPfor callers without an instance context.core.New.Working principle
Instance-owned internal paths now obtain their DNS client and outbound manager from the current Xray context.
The context contains a lazy provider rather than eagerly captured feature references because contexts are created while an instance may still be under construction. The provider resolves the current DNS client and outbound manager from that specific instance when they are needed.
Starting another core may still update the process-wide compatibility fallback, but it no longer changes the dependencies used by context-aware paths belonging to an existing instance.
Legacy callers without an instance context continue to use the synchronized process-wide fallback.
Scope and compatibility
InitSystemDialerandLookupForIPAPIs remain available.Validation
The affected packages pass: