tempdir: return cleanup functions instead of launching goroutines
This MR aims to solve the issue: #4760 (closed)
Closes !5476 (merged) and continues the implementation with updated approach based on maintainer feedback.
The tempdir package is launching a goroutine every time a temporary directory is created. The purpose of the goroutine is to clean up the temporary directory on context cancellation. This doesn't seem very necessary and has the following downsides:
- On our tests, we look for goroutines that are still running after tests. This deletion goroutine is not synchronized with this and can thus cause flakes.
- If context is canceled, the directory is deleted. If there is still code operating on the directory, that code can fail with unexpected errors rather than returning a context canceled error.
So instead of using goroutines, make the tempdir.New* functions return a cleanup function as a third return value. This approach makes it harder to miss cleanup, as the compiler will error about an unused variable. The cleanup function must be called explicitly to remove the directory, ensuring cleanup happens on all code paths including error scenarios.