I've got a perplexing problem.
I have a function F which takes an IRP, and does some stuff and completes the IRP after setting the Status and Information fields.
This occurs within a DeviceControl routine.
In some cases, there is an error in the request it is processing, and it then puts STATUS_INVALID_PARAMETER and 0 in the IRP fields.
If F is called directly by DeviceControl, and there is an error, things are fine, and the user client gets an error code of 'the parameter is incorrect'.
However, I can also call F in a DPC's deferred routine. DeviceControl returns STATUS_PENDING to the system, and the F completes the IRP within the DPC.
If F is called within the DPC and there is no error, things work fine, and I get the correct data back to the user client.
But, if F is called within the DPC and there is an error, the driver hangs. I cannot do SC STOP on the driver, and the user client hangs waiting for the DeviceControl function to return, and I cannot kill the process.
So apparently, the IoCompleteRequest isn't working right.
Is there a possible race condition here, where the DPC executes as soon as it is queued and before the DispatchControl has a chance to return?