How does a CVR / FDR determine when to stop recording? For example, the CVR records only about 2 hours of data.

Overwriting important data must be avoided, and thus the recorders should know if a catastrophe has happened. Then they can stop recording (to protect the already recorded data from overwriting), and start signaling the rescue team.

What is the logic used to stop recording?
The CVR and FDR have impact switches to stop recording when they experience high acceleration. In incidents and accidents that don't involve high enough acceleration, the plane remains intact enough for the boxes to be immediately found and switched off manually. In more serious crashes, the boxes stop recording because of the physical destruction of everything but the memory units (only the memory units are built to survive crashes).