question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using a thread pool for pin state notifications can reorder events

See original GitHub issue

pi4j uses an internal thread pool to deliver PIN state events. Problem with this approach is that if your software is relying on the latest GpioPinDigitalStateChangeEvent.getState(), then, because the order of events in the thread pool is not guaranteed, your listener can end up having the wrong final event value, if the rate of event change is rapid enough.

Consider this PIN state change sequence:

1 LOW 2 HIGH 3 LOW

The final state is low, but if it so happens that the thread that delivers event (3) takes longer than the time difference between events (2) and (3), and the thread that delivers event (2) is faster, then the listener will see the events in this order:

1 LOW 3 LOW 2 HIGH

So the listener now erroneously thinks that the pin is in high state.

The workaround is to ignore the event value and always query the pin state in the event listener.

I think the idea of using a thread pool for pin state notifications, as opposed to a single thread, is not a good idea.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
savageautomatecommented, Jun 11, 2020

@ylexus ,

In my case, global static override via GpioFactory is not an option, as my software is a library. A library does not have a right to override the event handling pool for the whole JVM. I would suggest removing all global static overrides like that and instead provide an instance-bound configuration via GpioFactory, something like that:

GpioFactory gpioFactory = GpioFactory.builder()
    .executorServiceFactory(myExecutorServiceFactory)
    .build()

I think you will like the patterns we have in place for Pi4J V2. No static singletons. A Pi4J Context object is created (via builder pattern) and can be customized by the user for its runtime. All Pi4J references, lifecycles, threads, etc are all managed inside the context.

Here is an example/demo project: https://github.com/Pi4J/pi4j-demo-telegraph/blob/master/src/main/java/com/pi4j/demo/telegraph/Telegraph.java

This demo code also shows that I/O instances are created using builders. Lots of good stuff on the way …

1reaction
eitchcommented, Jun 11, 2020

@savageautomate I think we should change it for the 1.4 release as it has bad side affects. I will create a patch asap.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Thread Pool ETW Events - .NET Framework - Microsoft Learn
I/O thread pool events, which provide information about I/O threads that are created, retired, unretired, or terminated in the thread pool.
Read more >
Should Observers be notified in separate threads each one?
If you do not trust your observers, then create a thread pool and for each notification, create a task and submit it to...
Read more >
23 Using Event Notification - Oracle Help Center
If an event is mapped to more than one opcode in the event notification list, BRM runs the opcodes in the order they...
Read more >
Asynchronous Events in C# - CodeProject
Asynchronous Events using TPL. Using Task Parallel Library (TPL), we can make our EventHandler s run asynchronously on separate threads.
Read more >
Solved: Custom alerts for individual tomcat thread pools
Solved: I would like to receive an alert when the number of busy threads in a specific thread pool reaches a fixed threshold....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found