5 examples
Orphaned process
Process continues running after parent process ends, consuming resources.
[ FAQ1 ]
What is an orphaned process?
An orphaned process happens when a parent process ends before its child process, leaving the child running without its original parent. On Linux systems, orphaned child processes are automatically adopted by the
init
or systemd
process (usually PID 1
), allowing them to continue running in the background. On Windows, orphaned processes also continue running but without a parent-child relationship, making process management and cleanup more challenging. Although orphaned processes do not inherently consume excessive resources, their unintended presence can signal design flaws, resource leaks, or inefficient handling of processes within software applications.[ FAQ2 ]
How to fix orphaned processes
Fixing orphaned processes typically involves ensuring the parent process properly waits for or terminates its child processes before exiting. On Linux, you can explicitly manage child processes using system calls such as
wait()
or waitpid()
, ensuring all child processes complete before the parent terminates. If a parent process has already exited, manually terminating or restarting orphaned child processes may be necessary using commands like kill
or killall
. On Windows, use task management tools (Task Manager
) or command-line utilities (taskkill
) to manually terminate orphaned processes. Implementing robust error handling, process monitoring, and proper cleanup routines in your application code helps prevent orphaned processes from occurring in the first place.diff block
greptile
logic: API server PID file is created but never used for cleanup. Could lead to orphaned processes.
suggested fix
./target/release/server & # Run in background
echo $! > /tmp/api-server.pid # Store PID for later cleanup
+ trap 'kill $(cat /tmp/api-server.pid)' EXIT # Ensure cleanup on exit
diff block
greptile
style: No trap handler for background guardiand process, could leave orphaned process on script exit
suggested fix
+# Set up trap to kill background process on exit
+trap 'kill $(jobs -p) 2>/dev/null' EXIT
guardiand \
transfer-verifier \
evm \
--rpcUrl ws://eth-devnet:8545 \
--coreContract 0xC89Ce4735882C9F0f0FE26686c53074E09B0D550 \
--tokenContract 0x0290FB167208Af455bB137780163b7B7a9a10C16 \
--wrappedNativeContract 0xDDb64fE46a91D46ee29420539FC25FD07c5FEa3E \
--logLevel=info \
2> /tmp/error.log &
diff block
greptile
logic: Killing Raycast after spawning the relaunch subprocess could leave orphaned processes if kill fails. Consider reversing the order: kill first, then spawn relaunch.
diff block
greptile
logic: Sessions array cleared after stopAllSessions may leave orphaned processes if some stops failed
Want to avoid this bug in your codebase? Try Greptile.
Avoid this bug!