Error opening adapter: The handle is invalid

Feb 4, 2015 at 7:25 PM
Hi,

I'm using pcap.net in a multithreaded application. I'm opening the communicators (PacketDevice.Open()) with different BPF filters, in separate threads, then sending some packets and reading the captured response (with ReceivePacket() in a loop). After a while of running properly, the application hangs with the following exception message:
Unable to open the adapter. Adapter name: rpcap://\Device\NPF_{20...4C}. Error: Error opening adapter: The handle is invalid. (6)

Stack trace:    at PcapDotNet.Core.LivePacketCommunicator.PcapOpen(SByte* source, Int32 snapshotLength,
PacketDeviceOpenAttributes attributes, Int32 readTimeout, pcap_rmtauth* auth) in
c:\tfs\tfs.codeplex.com\tfs06\pcapdotnet\pcapdotnet\src\pcapdotnet.core\livepacketcommunicator.cpp:line 50
at PcapDotNet.Core.LivePacketCommunicator..ctor(SByte* source, Int32 snapshotLength,
PacketDeviceOpenAttributes attributes, Int32 readTimeout, pcap_rmtauth* auth, SocketAddress netmask)
in c:\tfs\tfs.codeplex.com\tfs06\pcapdotnet\pcapdotnet\src\pcapdotnet.core\livepacketcommunicator.cpp:line 31
at PcapDotNet.Core.LivePacketDevice.Open(Int32 snapshotLength, PacketDeviceOpenAttributes attributes, Int32 readTimeout)
in c:\tfs\tfs.codeplex.com\tfs06\pcapdotnet\pcapdotnet\src\pcapdotnet.core\livepacketdevice.cpp:line 75
code sample of the listener method:
public async void Listen(CancellationToken ct)
{
    Packet receivedPacket = null;

    using (var communicator = selectedDevice.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000))
    {
        using (BerkeleyPacketFilter filter = communicator.CreateFilter(myFilter))
        {
            communicator.SetFilter(filter);
        }
        while (!ct.IsCancellationRequested)
        {
            var captureResult = await Task.Factory.StartNew(() => communicator.ReceivePacket(out receivedPacket));
            if (receivedPacket != null)
            { PacketHandler(receivedPacket); }
        }
    }
}
After this exception occurs it looks like the winpcap driver crashes (eg. Wireshark can't be started) - only windows reboot helps.

What could cause this problem? Maybe my approach is wrong (ie. should I capture packets on multiple instances of communicators or not)?

Specs:
Windows2012r2 (on Windows2008r2 it seemed to work fine)
Pcap.Net 1.0.2 (76195)
WinPcap 4.1.3
Coordinator
Feb 6, 2015 at 12:27 PM
Hi WackoMastermind,

Can you explain why do you do
var captureResult = await Task.Factory.StartNew(() => communicator.ReceivePacket(out receivedPacket));
Instead of
var captureResult = communicator.ReceivePacket(out receivedPacket);
Boaz.
Feb 6, 2015 at 1:18 PM
I want this method to run asynchronously so I used the task-based async pattern here. ReceivePacket() reads data from the interface so it can take time (or not?).
Coordinator
Feb 6, 2015 at 1:27 PM
But isn't using "await" means that the main thread will be blocked on the "async" task?
Feb 6, 2015 at 1:44 PM
Edited Feb 6, 2015 at 1:45 PM
Yes, but the Listen() method is marked with 'async' keyword so when it's called from the main thread it will run in background/separate one.
public async void Listen(CancellationToken ct)
Anyway I don't think that's an issue here, because at the beginning I used Threads and later switched to async/await to see if that helps.
It looks like I forgot to mention that the problem occurs when I run several instances of the application (but only on Windows2012r2). Could you explain what causes that specific exception in PcapdotNet library?
Coordinator
Feb 13, 2015 at 10:39 AM
How many PacketCommunicators do you open in parallel?
Feb 17, 2015 at 10:47 AM
About 40 (8 per instance * 5 instances).

But I've managed to create an application that should crash the library. It somehow simulates my problem. Here's the source code https://gist.github.com/anonymous/4695e310f03f935e377f run 5 instances simultaneously (ie. using bat script) and it should instantly crash - after that - no tool using winpcap will work (eg. Wireshark)
Coordinator
Feb 20, 2015 at 6:46 AM
I've checked this and creating 1000 PacketCommunicators in a single thread is fine.
  1. Can you make the program you've written simpler? It seems to contain a lot of code with different ways to do multi-threaded that complicates things. If you can isolate the problem in a shorter code that could be helpful.
  2. It seems you never Dispose() the PacketCommunicator. This is important to do in order to free resources.
Boaz.
Mar 18, 2015 at 8:54 AM
Thanks for your help. It looks like adding mutexes everywhere on LivePacketDevice.Open fixed the problem.