liveBook · Manning

Execution context stack

As explained in Go in Practice, Second Edition

A call stack is the runtime structure that records active function invocations and their execution contexts.

An execution context stack records the active function calls within a goroutine. Each call to go creates a separate function stack, so concurrent handlers do not share a path for panic recovery. section 4.3.5

In a concurrent Go server:

  • listen accepts a connection and starts handle in a new goroutine. section 4.3.5
  • handle, response, and their nested calls occupy that goroutine's stack. section 4.3.5
  • A panic unwinds the current stack until recover runs; it cannot jump to the stack of the initiating function. section 4.3.5

Protecting a request handler

Wrap handle with defer and recover, log the recovered error, and close the connection. A reusable safely.Go wrapper applies the same boundary to arbitrary goroutine work.

How Go in Practice, Second Edition covers it

How deep it goes
Explains stack creation, unwinding, and recovery boundaries, but stops short of runtime implementation details.
Examples
Demonstrates an echo server, a panicky response, an HTTP handler, and a reusable goroutine recovery wrapper.
What it assumes
Assumes basic Go functions and goroutines while demonstrating defer and recover in server code.
What you can do after
Diagnose goroutine crashes and add deferred recovery at the correct function-stack boundary.
How it connects
Connects goroutines, function calls, panic, recover, defer, and HTTP request handling.

Also explained in

Compare how 8 books explain execution context stack →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.