Introduction
The download desk must close without cutting off the person already receiving its field notes. You will replace immediate connection closure with Go's ordinary HTTP shutdown coordination, then keep a real complete download from the original process.
This unit starts independently with the prepared handler and the same two documents. Bring the previous unit's precise PID and accepted-request observations, or equivalent experience, plus handlers, errors, goroutines and channels. Signal delivery, finite contexts, error matching and main's waiting responsibility are explained below. This covers ordinary HTTP, not WebSockets, distributed traffic switching or a production zero-downtime guarantee. Allow about 60 minutes; this is an author estimate.
Make Main Wait for Shutdown
In this step, implement finite shutdown coordination while preserving the prepared HTTP handler.
Start in Downloads and enter the independent workspace:
cd /home/labex/project/download-desk
cat lifecycle.go
Only lifecycle.go is yours to change. Keep the handler in main.go, both documents, go.mod, the prepared observers and their observations unchanged. The function currently captures SIGTERM but calls Close, which interrupts active connections. A signal handler alone does not make a service graceful.
An http.Server owns the listener and active HTTP connections. ListenAndServe blocks while accepting work. Shutdown first closes listeners and existing idle connections, then waits for ordinary active requests to become idle. Starting shutdown causes ListenAndServe to return http.ErrServerClosed immediately, even though accepted handlers can still be running. If main returns at that point, every goroutine disappears and the transfer is cut off. Therefore the main path must wait for Shutdown itself to return.
A context carries a cancellation or deadline budget. context.Background() supplies a starting context. context.WithTimeout(..., 45*time.Second) creates a finite 45-second shutdown budget and a cancel function; defer cancel() releases its associated resources when this function returns, even if work ends sooner. Cancellation is not proof of completion. The public maximum is 60 seconds after the stop request: if an accepted response cannot finish within the chosen budget, report an error and exit nonzero within that maximum. The reference uses 45 seconds; equivalent finite budgets no longer than 60 seconds are allowed. The check includes a finite long response that cannot finish within 60 seconds, so a shutdown using only context.Background() is not sufficient. In that isolated timeout trial, a partial transfer and a reported nonzero server exit are the correct failure outcome, not a successful drain. If the budget expires, Shutdown returns an error; our function returns that error and main exits nonzero. We do not label that case successful draining or silently restart the server.
signal.Notify(stopped, syscall.SIGTERM) asks Go to send that signal on a channel. This channel has buffer 1 because signal delivery must not wait for a receiver; one queued stop request is enough here. signal.Stop(stopped) cancels this channel's registration when the function ends; it is different from stopping the HTTP server. The buffered result channel similarly lets the serving goroutine report its one result while main is doing shutdown work.
The first select waits for either a serving error or a stop signal. That also prevents a bind/start error from being hidden forever behind a signal wait. After a successful shutdown, errors.Is(err, http.ErrServerClosed) recognizes the expected serving result. A different serving error is returned instead of being called success. runServer is called by main, so it returning only after shutdown is what keeps the process alive.
Before editing, predict three mistakes. If Shutdown runs in another goroutine and runServer returns as soon as serving stops, what keeps main alive? If you replace waiting with a fixed sleep, what happens when another accepted document takes longer? If shutdown has no deadline, what bounds a response that cannot finish? Keep the existing first select: it already lets a real startup error return instead of waiting forever for SIGTERM.
Edit the existing stopping function
Complete all editing, formatting and building in Downloads before starting a server attempt or a slow client. There is no timed request during this editing stage. You will change the short function you just read; no complete replacement function is supplied in this guided step.
First save a source backup. cp copies the file. The .bak extension is important: another file ending in .go would be compiled into this package and could define runServer twice.
cp lifecycle.go lifecycle.go.bak
Open the actual file in the terminal editor:
nano lifecycle.go
In nano, use the arrow keys to move and ordinary typing to insert text. Backspace and Delete remove characters. Ctrl means hold Control while pressing the other key. Ctrl+K cuts the current line; use it only when that whole line should be removed. Press Ctrl+W, type func runServer, then Enter to find the function. Ctrl+C cancels an unfinished search. The ^ in nano's shortcut labels means Control.
To save, press Ctrl+O, check that the filename is still lifecycle.go, then Enter. Ctrl+X exits. To discard unintended changes, press Ctrl+X, then N if asked whether to save. Ctrl+C cancels an unwanted filename prompt. If you opened the wrong file, exit without saving, use pwd to inspect your directory, repeat this step's cd, and reopen lifecycle.go. A wrong filename is not a reason to overwrite another file or reset the VM.
Work through the following decisions inside this existing file. Keep the function signature and the signal registration, serving goroutine, buffered channels and first select you already read. Those parts still have their own responsibilities.
First, extend the parenthesized import group with the standard-library packages context, time and errors, one quoted package name per line. Imports make those package names available; an unused import is a compiler error, so complete the corresponding calls before building. Preserve the existing HTTP and signal imports.
Next, find the immediate server.Close() operation after the stop case. That is the operation which cuts off active requests. Replace its immediate-close behavior with a finite shutdown sequence in the same main path. Create a context with context.WithTimeout(context.Background(), 45*time.Second) and keep both returned values in local variables, for example ctx and cancel. := declares and assigns those variables. Place defer cancel() after the context is created, so its resources are released on every subsequent return.
Then call server.Shutdown(ctx) directly and wait for its result in this function. Do not prefix this call with go: that would start work without making this main path wait. Check the returned error. If it is non-nil, return that actual error to main, which already prints it and exits nonzero. Go's if err := operation(); err != nil { ... } form keeps the operation's error local to that condition; you may also assign the result separately. Do not return nil when the deadline expires, and do not restart the server to hide a failed drain.
Finally, keep the receive from the buffered serving-result channel after a successful shutdown. <-result waits for that serving result. Use errors.Is(err, http.ErrServerClosed) to recognize the expected server-closed error rather than calling every serving error success. errors.Is returns a boolean; ! negates it. Return an unexpected error, and return nil only when shutdown succeeded and the serving result was the expected one. This result check cannot substitute for waiting for Shutdown: ListenAndServe can finish before active handlers do.
Before saving, trace the two paths in your own code: an early bind/serving error must return through the first select; a stop request must go through the finite shutdown and then inspect the serving result. Also trace the shutdown-error path: it must retain the error and release the context resources. Choose local names that make these responsibilities clear; the check accepts equivalent coordination rather than requiring the reference's variable names.
Save to the original filename and exit nano with the keys above. Read the file actually saved on disk:
cat lifecycle.go
Format the source using gofmt -w; -w writes the formatted file. Build the current package with an explicit executable path so a new terminal does not depend on a hidden PATH setting. -o desk names the executable and . selects this package:
/usr/local/go/bin/gofmt -w lifecycle.go
/usr/local/go/bin/go build -o desk .
A syntax or compilation error names a file and line. Reopen nano lifecycle.go, find the nearby statement with Ctrl+W, and repair that small region. Save, exit, inspect the file and repeat formatting and building. Keep the logic that already works; do not restore the entire original draft over your progress. If a file is missing, use pwd and the first cd to recover the directory. No global shell change or VM reset is needed.
Use Verify for this step after the build succeeds, before beginning the timed download in the next step. The check builds your unchanged function in isolated temporary fixtures, varies real download durations and completion order, and exercises the long-response failure branch as well as normal success. Allow about a minute for it to finish. A failure is evidence to compare with your two control-flow paths, not permission to overwrite a completed download or erase observations. The check does not signal your live desk, change your source, or supply a passing implementation.
Keep the Download and Close the Listener
In this step, keep an accepted real download while refusing new connections and waiting for the original server.
Switch to Server, enter the workspace and start a new observed attempt. This terminal stays occupied until the original process ends:
cd /home/labex/project/download-desk
python3 desk.py serve drained
Wait for its actual ready message, then switch to Downloads. Read this entire step, including the expected probe failure and recovery, before starting the following finite slow transfer. Then run the blocks in sequence; do not use the transfer window to learn a new concept. Start a new finite slow transfer in the background, just as in the previous unit. The 300ms between 32-byte writes gives ordinary reading time; it is not an unbounded wait for a signal.
python3 desk.py download drained completed north-pier.txt 300 & download_job=$!
python3 desk.py accepted drained completed
The observer reads the real accepted request before you stop anything. Obtain and inspect the original built process identity, then send SIGTERM from this terminal:
server_pid=$(python3 desk.py pid drained)
python3 desk.py identity drained
kill -TERM "$server_pid"
While that accepted download is still in progress, test a new connection. The prepared probe makes bounded real connection attempts for up to two seconds, retaining every native outcome until a refusal appears. This allows Go to handle the delivered signal before the first failed connection is observed; the probe does not assume kill synchronously closes the listener. The next command is expected to return 7, with a connection-refused message:
python3 desk.py probe drained
This is a refusal to create a new connection, not an HTTP 403 or a damaged accepted transfer. The original download should continue. Wait for its background job and compare its saved actual bytes with the original:
wait "$download_job"
cmp documents/north-pier.txt downloads/completed.txt
cat downloads/completed.txt
Expect native curl 0, no cmp output, and the complete final chapter. Switch to Server: it should now report Original server native exit: 0. The server may finish sending before the client process finishes reading or flushing its file; shutdown does not wait for a client-side application acknowledgement. These are separate observations, not a required order between two terminal messages.
Return to Downloads. Retain lifecycle.go, the complete download, and the native observations. The check reads this live result and tests the same source on isolated random ports with an existing idle connection and accepted requests in different orders. Direct Close, returning from main while Shutdown is still running, keeping the listener open until work finishes, or sleeping for a guessed interval do not meet this contract.
If the download has already completed before accepted or the stop action, preserve its file and choose a new label on the still-running attempt. If the server has already exited, explicitly start a new attempt name in Server and use that name for a new slow output and the identity, accepted and probe commands. A refused probe taken only after the process exited does not show early listener closure; retry the observed active workflow with new names. Never overwrite the successful earlier file or reset observation history.
For a complete retry after the previous attempt has exited, use Server:
python3 desk.py serve drained-retry
After the ready message, use Downloads. Read all blocks first. These new names preserve the earlier files and observations:
python3 desk.py download drained-retry completed-retry north-pier.txt 300 & download_job=$!
python3 desk.py accepted drained-retry completed-retry
server_pid=$(python3 desk.py pid drained-retry)
python3 desk.py identity drained-retry
kill -TERM "$server_pid"
The following probe is expected to return 7:
python3 desk.py probe drained-retry
wait "$download_job"
cmp documents/north-pier.txt downloads/completed-retry.txt
Expect curl 0, equal complete bytes, and original native exit0 in Server. If the earlier server is still running, finish reading before starting a fresh slow output on that attempt; use a new label in both download and accepted, and retain that attempt name for identity and probe. If an earlier probe already exists, preserve it, explicitly stop your own old attempt and let it exit before the complete new-attempt retry above.
A genuine startup error appears in the server's existing stderr and returns nonzero; correct a documented source/build mistake or wait for your own previous attempt to exit before using 8080 again. Do not kill a same-named platform process. A shutdown deadline error means the finite budget did not establish completion; read it honestly rather than substituting a replacement process or copying an earlier document.
Summary
You coordinated the listener, accepted HTTP handlers, a finite shutdown context, and main. The original process stopped accepting connections while finishing its existing work. Exact final bytes and native outcomes made the result observable.



