Skip to content

Fix races in Android process and asset callbacks - #215

Open
OneMiny wants to merge 1 commit into
2dust:mainfrom
OneMiny:fix/android-process-and-asset-callback-races
Open

OneMiny wants to merge 1 commit into
2dust:mainfrom
OneMiny:fix/android-process-and-asset-callback-races

Conversation

@OneMiny

@OneMiny OneMiny commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Problem

RegisterProcessFinder replaces the process callback in Xray while routing can read it. On the official build, concurrent registration and unregistration caused a nil-pointer panic in one of ten stress runs.

InitCoreEnv also replaces Xray's global file reader while another goroutine may be reading a file. Go's race detector reported a read of filesystem.NewFileReader concurrent with the write in InitCoreEnv.

Fix

  • Install the Xray process and file callbacks during package initialization. Use a typed atomic pointer to publish the current finder, and an atomic boolean to enable the APK asset fallback at the end of InitCoreEnv.
  • Load the finder once per lookup. The Java callback can replace or unregister itself during a call. An in-flight lookup keeps its snapshot, and subsequent lookups see the new registration.
  • Try the file on disk once. Fall back to the packaged asset when the file is missing. Preserve other errors and the existing Go and Java method signatures.
Check Before After
File reads concurrent with InitCoreEnv Go's race detector reported a race on the file reader Passed 20 runs with race detection
Concurrent finder registration and lookup on Android 12 One of ten runs reported a nil-pointer panic All ten runs passed
Finder unregisters itself from inside the callback Passed Passed
Symlink loop on disk The reader returned ELOOP Same error
Java finder in SOCKS routing and certificate and key fallback from APK assets Both passed through JNI Both passed through JNI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant