Fix SearchContext from being closed prematurely

Fixes SearchContext from being closed during initialization or immediately
after processing is started

Closes #5165
Add test and randomization for #5165