Did you give the code changes a test in the end? The original code makes some “checks” that might result in the issue you are facing and removing those checks (thus simply using the code posted above) might resolve that. I unforunately can not reproduce this issue on my end yet (even with the original code). Does it happen for you always on some specific references or is it completely random?