Add cleanup logic to worker.ts (#54500)
This implements the same cleanup logic used for start-server and render-workers for the workers used during build.
It's more of a contingency as we do call `.end()` on the worker too.
Fixes #45508
Co-authored-by: Zack Tanner <1939140+ztanner@users.noreply.github.com>