calimero-project / calimero-core

Core library for KNX network access and management
Other
129 stars 65 forks source link

Is calimero thread safe? #14

Closed Snickermicker closed 9 years ago

Snickermicker commented 9 years ago

Sometimes, when the connection to the network is lost I see a thread being 'stuck' in method quit() of class tuwien.auto.calimero.link.EventNotifier. To get details of where that happens I added some log output to that method and it seems to be the join() call.

The method I used to produce the error is to shutdown the network interface.

11:02:46.268 ERROR tuwien.auto.calimero[:39] - KNXnet/IP Tunneling 192.168.1.251:3671: send disconnect failed
java.io.IOException: Das Argument ist ungültig
    at java.net.PlainDatagramSocketImpl.send(Native Method)
    at java.net.DatagramSocket.send(DatagramSocket.java:697)
    at tuwien.auto.calimero.knxnetip.ConnectionBase.close(ConnectionBase.java:465)
    at tuwien.auto.calimero.knxnetip.ClientConnection$HeartbeatMonitor.run(ClientConnection.java:429)
11:02:46.288 ERROR tuwien.auto.calimero[:39] - KNXnet/IP Tunneling 192.168.1.251:3671: close connection - heartbeat communication failure
java.io.IOException: Das Argument ist ungültig
    at java.net.PlainDatagramSocketImpl.send(Native Method)
    at java.net.DatagramSocket.send(DatagramSocket.java:697)
    at tuwien.auto.calimero.knxnetip.ClientConnection$HeartbeatMonitor.run(ClientConnection.java:406)
11:02:46.290 INFO  tuwien.auto.calimero[:43] - process link 192.168.1.251:3671: attached link was closed
11:02:50.045 INFO  tuwien.auto.calimero[:43] - link 192.168.1.251:3671: Current Thread: Thread[KNXnet/IP heartbeat monitor,10,main]
11:02:50.046 INFO  tuwien.auto.calimero[:43] - link 192.168.1.251:3671: this: Thread[Link notifier,10,main]
11:02:50.046 INFO  tuwien.auto.calimero[:43] - link 192.168.1.251:3671: join() requested
11:02:50.047 INFO  tuwien.auto.calimero[:43] - link 192.168.1.251:3671: link closed
    final void quit()
    {
        interrupt();
        if (currentThread() != this) {
            logger.info("Current Thread: "+currentThread());
            logger.info("this: "+this);
            try {
                logger.info("join() requested");
                join();
                logger.info("join() succeeded");
            }
            catch (final InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

Same happens btw when I started a separate thread which frequently read from the KNX bus using calimero. When the connection to the bus is lost, that thread is 'stuck'. Any pointers as to what I'm doing wrong?

calimero-project commented 9 years ago

The join provides a controlled sequence during shutdown. It waits for the EventNotifier to finish its round of close notifications. So my first two suspicions would be that either a notification does not return (you can check that in method fire like you did in quit), or a listener swallows the thread interrupt somewhere. The first two errors in your output are expected, indicating the network error (with Linux giving the rather bland error Invalid Argument).

Snickermicker commented 9 years ago

Thx a lot. I'll have a look.