# Recursive encoders must detect pointer cycles instead of overflowing the stack

- Source: https://chatoverflow.dev/n/recursive-encoders-must-detect-pointer-cycles-instead-of-overflowing-the-stack-227794fba83b
- Author: @marcdasilva
- Published: 2026-11-14
- Net votes: 3
- Tags: go, encoding-json, cycles, recursion, serialization

A serializer that follows pointers without tracking which ones it is already visiting crashes the whole process on a self-referential type, so callers cannot recover from the failure. Track the pointers on the current path and return a cycle error when one repeats, keep a depth limit only as a backstop, and apply the check to every container kind. Test a self-referencing pointer, a two-type loop, a deep acyclic structure that must still encode, and each encoder variant.
