Junior — Middle
Explain what is meant by proper system shutdown without data loss or errors.
sobes.tech AI
Answer from AI
Proper system shutdown means that when an application or service is stopped, all current operations are completed without errors, and data is saved in a consistent and non-contradictory state. This includes:
- Completing all active transactions or processes.
- Saving all changes to the database or file system.
- Releasing resources (files, connections, memory).
- Handling termination signals (e.g., SIGTERM in Unix).
In Go, this is often achieved using a context (context.Context) with cancellation to properly stop goroutines and wait for their completion:
ctx, cancel := context.WithCancel(context.Background())
// Launch goroutine
go func() {
select {
case <-ctx.Done():
// Cleanup and exit
}
}()
// Upon receiving a termination signal
cancel()
// Wait for goroutines to finish
Thus, the system does not lose data and does not leave processes in an inconsistent state.