When Application closes, driver again create a new device instance

Hi,

I have umdf2 virtual serial driver. Application connect to it and when application is closed the driver function EvtDeviceAdd is being called again creating new queue and everything. This is happening ONLY when i have added below code and accepted Open request in EvtFileCreate.

WDF_FILEOBJECT_CONFIG_INIT(&stFileObjectConfig,
EvtFileCreate,
EvtFileClose,
EvtFileCleanup);

WdfDeviceInitSetFileObjectConfig(DeviceInit,
                                &FileObjectConfig,
                                &DeviceAttributes);

May i know what is the reason behind this?

My aim with above code snippet was to get notified when Application open serial device.
I want to get notified up on Device open by application, but not EvtDeviceAdd is being called(second call to EvtDeviceAdd reset by underlying communication channel)

Any help would be really appreciated.

Either the underlying bus is recreating the device due to a bus error (unlikely) or your driver is crashing in the new callbacks you are setting and the driver is being restarted (most likely root cause).

1 Like

Thanks for reply.

This is a virtual serial driver and hence, i think, no bus error.
The application could successfully opened the device using CreateFile API, and does not doing anything with device.
And after some time (say 10 secs). it just exist.

But without below code snippet in Driver, this is not happening:
WDF_FILEOBJECT_CONFIG_INIT(&stFileObjectConfig,
EvtFileCreate,
EvtFileClose,
EvtFileCleanup);

WdfDeviceInitSetFileObjectConfig(DeviceInit,
&FileObjectConfig,
&DeviceAttributes);

Body of EvtFileCreate:
void EvtFileCreate(stRequest)
{
WdfRequestComplete(stRequest, STATUS_SUCCESS);
}

And what does the code for EvtFileClose and EvtFileCleanup look like? If the add device call is happening when the handle is closed, the driver crash is most likely in one of these two callbacks. Have you attached a debugger to the driver process before the handle is created and step through the relevant callbacks as they run?

Thanks!
Both debugging purpose, i made EvtFileClose, EvtFileCleanup to be empty.

Attach a debugger and see if the driver is crashing. A crash will cause an auto restart and you will see AddDevice run again.

Ok I attached debugger and found a crash. Your conclusion was right.

The callback EvtDeviceCleanup is invoked by framework when Application closed the driver handle. Logic written there was really meant for clean-up specific to device removal. But when it was called when application closed device handle, the same function executed but with NULL pointer for Context object.

But i would ask how to distinguish between whether clean-up is specific to application closing device handle or device removal?

EvtDeviceCleanup Is called when the device removed, it is not called when the handle is closed. You have another bug in your driver. Perhaps it is not an explicit crash, but something the driver is doing incorrectly is causing the underlying framework to tear down the device stack.

1 Like

The callback EvtDeviceCleanup is invoked by framework when Application closed the driver handle.
Logic written there was really meant for clean-up specific to device removal.

You have mixed up several concepts in that simple paragraph.

There is no standard “EvtDeviceCleanup” callback. Is that your WDFDEVICE’s context cleanup routine? That gets called when the device itself is removed, either through an unplug or through a restart. This is not directly related to application activity.

Applications do not open “driver handles”. They open device handles. EvtFileCleanup gets called when the application closes a device handle. That does not cause the device to be removed.

To catch the device being removed, you can use EvtDeviceD0Exit or EvtDeviceCleanup. They happen at different stages in the shutdown.

1 Like

There are two similarly named callbacks that are easy to confuse:

There’s the EvtFileCleanup, that you specify as part of your WDF_FILEOBJECT_CONFIG. This gets called (for all practical purposes) when the user calls CloseHandle.

There’s also the EvtCleanup callback that can be specified for any WDF Object, that’s specified in your WDF_OBJECT_ATTRIBUTES structure prior to object instantiation. This callback is called when the last handle is closed to the object.

Maybe that’ll help??

Peter

1 Like

Thanks Guys! I should have used the word closing device handle, instead closing driver handle. I had some confusion with respect a to file cleanup and device cleanup. Now it is cleared. Thanks.