make cycle detection on autosave works on a thread level

Fixes `stack level too deep` error when creating a children object on a parent callback.
This error was only happening when the inverse_of relation was set.

[related #12413]

review @rafaelfranca @tenderlove

(I will submit another patch for 3.2 in a second)