YsummarY, use Tab ↹, Return/Enter and go back (⌘ + ←) to navigate.

GopherCon 2015: A Practical Guide to Preventing Deadlocks and Leaks in Go - Richard Fliam

YouTube Video

This GopherCon talk focuses on preventing deadlocks and leaks in Go applications using Communicating Sequential Processes (CSP). Key takeaways include:

Understanding and Applying CSP:

  • Data Flow, Not Control Flow: Design processes focusing on data flow (inputs and outputs) rather than control flow. Avoid “manager-driven” workflows and excessive signaling. Control flow belongs within individual processes, not between them.
  • Connection Diagrams: Visualize the system’s processes and communication using connection diagrams (boxes for processes, arrows for channels). This helps identify potential problems like god processes (overly complex processes), rat’s nests (highly interconnected processes), and cycles (potential deadlocks).
  • Pipelines: Structure the system as a series of unidirectional pipelines. These are directed acyclic graphs of processes that prevent deadlocks. Many seemingly complex systems can be redesigned as pipelines.
  • Exit Strategies: Crucial for preventing both deadlocks and leaks. Use context.Context and its Done channel for clean and safe process termination. Remember to collect all results before exiting to avoid leaks. If using contexts, avoid uncancellable operations. Always select on the context’s Done channel.

Practical Advice for Avoiding Leaks:

  • Stop Tickers: Explicitly stop timers when they’re no longer needed.
  • Cancel Contexts: Call context.Cancel() when finished with a context (often using defer).
  • Avoid defer in Infinite Loops: This can prevent the Go runtime from garbage collecting, leading to memory leaks.
  • Finish Goroutines: Ensure all goroutines complete before the program exits to avoid leaks.

The talk uses examples like an MPEG-TS demuxer to illustrate these principles. The speaker emphasizes that applying these CSP design patterns simplifies complex concurrent systems and reduces the risk of deadlocks and memory leaks.

Next: Inside the Massive $100M SAP S/4HANA Failure at Spar Group
Prev: Dave Farley on remote work, AI, and the future of software engineering