Host function callbacks can deadlock when calling back into the sandbox
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 42/100
- Tipo de issue
- Error
- Claridad
- Bastante claro
- Estado de actividad
- Tranquilo
- Stack tecnológico
- javascript, rust
- Área
- backend
Línea de trabajo
Empieza rastreando el bloqueo del sandbox a través de call_handler y del dispatch de host-functions en handle_event; después, compara el enfoque de bloqueo separado en src/hyperlight_host/src/sandbox/outb.rs. Reproduce el callback usando registerHostFunction y callHandler, y revisa el executing_flag de PR #55. Se considera terminado cuando los callbacks puedan llamar a operaciones del sandbox sin provocar un deadlock, con cobertura para la reproducción reportada.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Problem
When a host function callback (registered via registerHostFunction or setHostPrintFn) tries to call back into the same sandbox (e.g. callHandler, snapshot, restore, unload), it deadlocks.
This happens because call_handler holds the LoadedJSSandbox mutex for the entire duration of guest execution. Host functions are dispatched via TSFN to the Node.js main thread while that lock is held. If the callback then calls any method that needs the same lock, it waits forever.
Why this doesn't happen in core hyperlight
In hyperlight-dev/hyperlight, the host function registry (Arc<Mutex<FunctionRegistry>>) uses a separate lock from the sandbox. Host functions are dispatched synchronously while the VM is paused — they don't need the sandbox lock at all. See src/hyperlight_host/src/sandbox/outb.rs.
In hyperlight-js, the QuickJS runtime invokes host function closures inside handle_event, which requires &mut self on the sandbox. The NAPI layer wraps this in a single tokio::sync::Mutex, so host function dispatch and sandbox lifecycle share the same lock.
Current workaround
PR #55 adds an executing_flag (AtomicBool) that detects reentrancy at runtime. If a callback tries to acquire the lock while guest code is executing, it returns ERR_REENTRANT instead of deadlocking. This prevents hangs but doesn't allow the operation to succeed.
Suggested fix
Separate host function dispatch from the sandbox lock, similar to how core hyperlight does it. Options:
- Move host function state out of the
&mut selfborrow so callbacks don't need the sandbox lock - Temporarily release the sandbox lock before dispatching to host functions, reacquire after
- Provide a shared FFI/binding helper crate that handles this pattern correctly for any language binding
Reproduction
const loaded = await sandbox.getLoadedSandbox();
proto.registerHostModule('mymod', (mod) => {
mod.registerHostFunction('callback', async () => {
// This deadlocks (or returns ERR_REENTRANT with the fix)
await loaded.callHandler('other_handler', {});
return 'result';
});
});
- Lenguaje dominante
- Rust
- Estrellas
- 13
- Forks
- 5
- Merge medio
- 8 d 4 h
- PR fusionados (30 d)
- 14
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de hyperlight-dev/hyperlight-js
-
lifecycle/needs-review
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
-
lifecycle/needs-review
Dificultad 5/5 Más de una semana Aptitud para principiantes 35/100
hyperlight-dev/hyperlight-js#312 ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 45/100
hyperlight-dev/hyperlight-js#252 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 45/100
hyperlight-dev/hyperlight-js#249 ·
-
lifecycle/needs-review
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
hyperlight-dev/hyperlight-js#223 ·
Todos los issues de hyperlight-dev/hyperlight-js
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
TheLarkInn/aipm#2413 ·
-
documentation
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
alexgorbatchev/simple-ptt#15 ·
-
tooling
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
-
todo:ticket
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
taikoxyz/taiko-mono#22168 · 1 comentario ·