logoalt Hacker News

lawrjone01/22/20251 replyview on HN

This guidance is actually super important, as contexts are expected to be modified in a code flow and apply to all functions that are downstream of your current call stack.

If you store contexts on your structs it’s very likely you won’t thread them correctly, leading to errors like database code not properly handling transactions.

Actually super fragile and you should avoid doing this as much as is possible. It’s never a good idea!


Replies

dfawcus01/23/2025

What do you mean by "won't thread them correctly"?

The specific use case I had was where the context represented the lifetime of a (shared) TCP connection. I then wanted to use its cancellation to drive the destruction of various dynamic graph elements hanging off that shared connection.

Think a graph of muxes/demuxes in a dynamic message graph while the whole program is a CSP style thing.

I needed something to drive destruction, the context provided what I needed.