Giter Club home page Giter Club logo

Comments (7)

kyle-github avatar kyle-github commented on July 22, 2024 1

Once I make the API threadsafe I would like to see if you can recreate this. At that point you should be able to stop using the tag lock/unlock API calls unless you are chaining multiple calls.

from libplctag.

kyle-github avatar kyle-github commented on July 22, 2024

It is a long log file :-)

I am trying to figure out what is happening here. There is a lot of threading going on and it looks like there are cases where the tag is passed among threads. The log file ends in the middle of output from the background IO thread, so I assume that is where the crash happened. However, before that there is some stuff that I cannot figure out because I do not know how your apps works.

In the library log entries, there is an indicator on each log line of which thread wrote the log. At the very end of the log file, thread 24 (the numbers are simply assigned in order that the threads are seen) is active. However, what it does seems odd.

Thread 24 first shows up here:

thread(0024) 2017-04-01 14:58:32.892 INFO plc_tag_unlock:321 Starting.
thread(0024) 2017-04-01 14:58:32.892 INFO plc_tag_unlock:331 Done.
thread(0024) 2017-04-01 14:58:32.892 INFO plc_tag_destroy:475 Starting.
thread(0024) 2017-04-01 14:58:32.892 INFO plc_tag_destroy_mapped:413 Starting.
thread(0024) 2017-04-01 14:58:32.892 WARN mutex_destroy:426 error while attempting to destroy mutex.
thread(0024) 2017-04-01 14:58:32.892 WARN release_tag_to_id_mapping:1567 Tag not found or entry is already clear.

Note that the first thing is does is unlock the mutex for the tag. That means that some other thread somewhere else created the tag and locked the mutex. That seems dangerous. Then the following errors indicate that the tag is already destroyed. The error from mutex_destroy shown seems to indicate that there is another thread with the mutex locked and thus the destruction of the mutex errors out.

At least from this perspective, it looks like there is a logical flow problem in the app code somewhere. Having one thread lock a mutex and a different thread unlock it means that you could easily run into situations where the first thread is still using the mutex or some third thread is waiting on it.

I will see if I can do a few tweaks to the code to try to find out more. I do not have good logging telling me which tag is being operated on and I would like to find that out here to see if my hypothesis is correct.

All that said, nothing you do should be able to cause a crash in the library. I will also look at tightening up some of the mutex handling on plc_tag_destroy to see if I can remove any possibility of multiple threads hitting that code at the same time.

from libplctag.

xinthose avatar xinthose commented on July 22, 2024

I see. I will review my code and my error handling with the mutexes. Thank you.

There are 3 PLC's in this scenario.

from libplctag.

kyle-github avatar kyle-github commented on July 22, 2024

I ran across a comment about the POSIX specification about mutexes.

According to Posix "If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, undefined behavior results."

See here: [http://www.realworldtech.com/forum/?threadid=168856&curpostid=168883](Real World Tech)

So, this may explain some of what you are seeing.

That said, clearly I just need to make the library work in a threadsafe manner. It does not make sense to have all potential users pay the price when the library could do it internally. It is not fun to debug though :-(

from libplctag.

xinthose avatar xinthose commented on July 22, 2024

That sounds good. I assumed the library was thread safe through the use of plc_tag_lock and plc_tag_unlock. Is there a way to check to see if a tag is locked before I try to unlock it? In my close_all_tags function I unlock all tags then destroy them, without checking if they're locked or not first.

from libplctag.

kyle-github avatar kyle-github commented on July 22, 2024

Oops, I missed your reply. Sorry about that!

Unlocking mutexes is another one of the wonderful things that POSIX does not help with. As the quote says, unlocking and unlocked mutex results in undefined behavior. Sigh.

In practical terms, I do not think I have run across an implementation that crashed. It usually returns an error I think.

The library is thread safe now. If your only use of the lock/unlock API calls was to guard against threads hitting the individual API calls, then you should be able to remove all of those extra calls.

from libplctag.

xinthose avatar xinthose commented on July 22, 2024

OK. Thank you. I use a scoped lock on the class functions now to protect against errors and crashes.

from libplctag.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.