NokiaTube | New address | Mode Phone | On high quality
Skip to content

Windows: Script.unload() during a process exit hangs both the unload and the exit (17.24.x; not in 17.23.3) #1192

Open

Description

@Albasha0x0

On Windows, unloading a script and detaching while a thread that was held in an Interceptor.attach hook runs on into the process exit hangs: script.unload() does not return and the process never finishes exiting (or the unload returns and the exit never finishes). It started between 17.23.3 and 17.24.0.

Repro

Build the target with cl /Od target.c (the source is below). Then run python -I race.py target.exe 100 0.

  1. Spawn target.exe, attach, and load a script with Interceptor.attach(work, { onEnter() { send('hit'); recv('go', () => {}).wait(); } }). Then resume.
  2. On the hit message, post go, then immediately call script.unload() and session.detach(). The released thread returns from work(), and main returns into RtlExitUserProcess.
  3. Wait up to 10 s for the unload and detach, and for the process to end.

Arguments: the third one is 1 to also attach to ntdll!RtlExitUserProcess. Two controls: ... 100 0 after-exit unloads once the process has exited, and ... 100 0 nohold has no recv().wait() in the hook.

Observed

Windows 11 x64 (build 26200), Python 3.12.4 x64, x64 target, 100 runs each.

Frida Hung runs
17.24.3, 100 0 7 and 12 of 100
17.24.3, 100 1 13 of 100
17.24.0 3 of 100
17.23.3, 17.23.2, 17.23.1 0 of 100 each
17.23.0 0 of 150
17.22.0 0 of 250
17.19.0, 17.18.0, 17.12.0, 17.0.7, 16.7.19 0 of 100 each

Controls on 17.24.3: unload after the process has exited, 0 of 100; thread not held in the hook, 1 of 100.

Expected

The unload and detach either complete or fail with "process terminated", and the target finishes its exit.

Range

The regression is between 17.23.3 and 17.24.0. These frida-gum commits in that range touch this path:

  • 7822612 "interceptor: Defer freeing detached listener entries"
  • f8d4782 "windows: Track dependency DLLs in module registry"
  • ce7a26e "gumjs: Sweep dead weak refs when leaving a scope"

I found no existing report. Searched: detach hang, unload hang, exit hang, ExitProcess, script.unload.

target.c

#include <stdio.h>
__declspec(dllexport) __declspec(noinline) int work(int n) { return n * 2 + 1; }
int main(void) { int r = work(20); printf("%d\n", r); fflush(stdout); return 0; }

race.py

# A thread held in an Interceptor hook is released, then the script is unloaded and the session detached
# while that thread runs on into the process exit. Counts slow unloads and exits that do not finish.
import ctypes, sys, threading, time
import frida

k32 = ctypes.WinDLL("kernel32", use_last_error=True)
k32.OpenProcess.restype = ctypes.c_void_p
k32.WaitForSingleObject.argtypes = [ctypes.c_void_p, ctypes.c_uint32]
k32.TerminateProcess.argtypes = [ctypes.c_void_p, ctypes.c_uint32]
k32.CloseHandle.argtypes = [ctypes.c_void_p]
SYNCHRONIZE, TERMINATE = 0x00100000, 0x0001

AGENT = """
const work = Process.getModuleByName('target.exe').getExportByName('work');
Interceptor.attach(work, { onEnter() { send('hit'); if (HOLD) recv('go', () => {}).wait(); } });
%s
"""
EXIT_HOOK = """
const exitUser = Process.getModuleByName('ntdll.dll').getExportByName('RtlExitUserProcess');
Interceptor.attach(exitUser, function () { send('exiting'); });
"""

def once(target, exit_hook, delay, after_exit=False):
    device = frida.get_local_device()
    pid = device.spawn([target], stdio="pipe")
    handle = k32.OpenProcess(SYNCHRONIZE | TERMINATE, False, pid)
    session = device.attach(pid)
    hold = "nohold" not in sys.argv
    script = session.create_script(("const HOLD = %s;" % ("true" if hold else "false")) + AGENT % (EXIT_HOOK if exit_hook else ""))
    hit = threading.Event()
    script.on("message", lambda m, d: hit.set() if m.get("payload") == "hit" else None)
    script.load()
    device.resume(pid)
    if not hit.wait(10):
        raise RuntimeError("no hit")
    script.post({"type": "go"})
    if after_exit:
        k32.WaitForSingleObject(handle, 10_000)
    elif delay:
        time.sleep(delay)
    t0 = time.perf_counter()
    result = "ok"
    done = threading.Event()
    step = ["unload"]
    def unload():
        try:
            script.unload()
            step[0] = "detach"
            session.detach()
        except frida.InvalidOperationError:
            pass
        done.set()
    threading.Thread(target=unload, daemon=True).start()
    if not done.wait(10):
        result = step[0] + " hung"
    unload_ms = (time.perf_counter() - t0) * 1000
    if k32.WaitForSingleObject(handle, 10_000) != 0:
        result = result + ", exit hung" if result != "ok" else "exit hung"
        k32.TerminateProcess(handle, 1)
    k32.CloseHandle(handle)
    return result, unload_ms

if __name__ == "__main__":
    target, runs, exit_hook = sys.argv[1], int(sys.argv[2]), sys.argv[3] == "1"
    after_exit = len(sys.argv) > 4 and sys.argv[4] == "after-exit"
    delays = [0, 0.0005, 0.001, 0.002, 0.005]
    bad, slow = 0, 0
    for i in range(runs):
        r, ms = once(target, exit_hook, delays[i % len(delays)], after_exit)
        if r != "ok": bad += 1
        if ms > 1000: slow += 1
        if r != "ok" or ms > 1000:
            print(f"run {i}: {r}, unload {ms:.0f} ms, delay {delays[i % len(delays)]}", flush=True)
    print(f"frida {frida.__version__}: {runs} runs, {bad} hung, {slow} unloads over 1 s", flush=True)

Activity

  1. Albasha0x0 commented on Oct 22, 2026

    @Albasha0x0
    Author

    More data on this hang: it also happens on ARM64 Windows, both natively and with emulated x64, and it is still there in 17.24.3. Same sequence as in #1194: a thread held in a hook is released, then script.unload() runs while that thread goes on into the exit.

    windows-11-arm, Python 3.13 ARM64, the close_at_exit.py repro from #1194:

    • 17.24.3, x86 / x64 (both emulated) / arm64 targets, 600 closes each: 13 / 5 / 29 hung with a thread observer attached, 7 / 2 / 11 hung with it detached first. With no observer, the arm64 target hung 7 times in 300 closes.
    • 17.23.3: 0 hung in 5400 closes.

    In a full dump of a hung x64 target (17.24.3), the main thread waits on an SRW condition variable inside frida_exit_monitor. The agent's runner thread is blocked on a condition variable. gmain, gdbus and gum-js-loop are idle in WaitForMultipleObjects.

  2. Albasha0x0 commented on Nov 4, 2026

    @Albasha0x0
    Author

    Retested on 17.25.2 (Windows 11 x64, Python 3.12, x64 target) with the race.py from this report: 0 hung runs in 400 (100 0, 100 1, 200 0). The same files on 17.24.3 on the same host still hang (1 of 100 in the control run). It looks fixed in 17.25.x. Do you know which commit fixed it? We have not checked ARM64 or an x86 target.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions