Synchronizing InstanceSetup with FilterUnload

Hello, I have a problem and I don’t know how to solve it.

I realized that the InstanceSetup is not properly synchronized with the Unload function and the InstanceSetup may be executing after the driver is unloaded => BSOD :).

I have searched online(especially on MSDN) to find out what happens with the threads executing the Instance Setup when the driver is unloaded(either when the register Unload function is called or when FltUnregisterFilter is called by the driver) and I couldn’t find an answer.

I can’t think of any way to ensure that the InstanceSetup functions are finished executing before the driver unloads.

I can appreciate any suggestions on how to solve the problem or directions to a more detailed documentation.