libplctag: PLCTAG_ERR_NOT_FOUND on a tag that should exist

I’d greatly appreciate some help on diagnosing something that happens but shouldn’t be possible 😉

I do something like this (pseudo-code):

bool write_tag(char* tag_name, int data_type, int count, void* data) {
    // Create tag
    int tag = plc_tag_create(tag_name, TIMEOUT);
    if (tag < 0) {
        handle_error();
        return false;
    }

    // Copy data to tag using set_value_to_tag

    // Register callback
    if (plc_tag_register_callback_ex(tag, tag_callback, callback_data) != 0) {
        plc_tag_destroy(tag);
        return false;
    }

    // Write to PLC
    if (plc_tag_write(tag, TIMEOUT) != 0) {
        // Callback will handle cleanup on error
        return false;
    }

    return true;
}

then in tag_callback, on PLCTAG_EVENT_WRITE_COMPLETED I do some processing in a thread from a thread pool (eg, I try to not block tag_callback), then call:

plc_tag_unregister_callback(tag_id);
plc_tag_destroy(tag_id);

The problem is that sometimes, not that often, I get a sequence like this (actual log):

tag(20) created for WRITE: protocol=ab-eip&gateway=10.0.20.14&plc=controllogix&path=1,0&elem_size=4&elem_count=1&name=Test_tag[1]&debug=2
tag(20) calling plc_tag_write: protocol=ab-eip&gateway=10.0.20.14&plc=controllogix&path=1,0&elem_size=4&elem_count=1&name=Test_tag[1]&debug=2
tag(20) PLCTAG_EVENT_WRITE_STARTED
tag(20) PLCTAG_EVENT_WRITE_COMPLETED
tag(21) created for WRITE: protocol=ab-eip&gateway=10.0.20.14&plc=controllogix&path=1,0&elem_size=4&elem_count=1&name=Test_tag[1]&debug=2
tag(21) calling plc_tag_write: protocol=ab-eip&gateway=10.0.20.14&plc=controllogix&path=1,0&elem_size=4&elem_count=1&name=Test_tag[1]&debug=2
tag(21) PLCTAG_EVENT_WRITE_STARTED
2024-11-20 12:40:49.962 thread(1) tag(12) WARN plc_tag_generic_tickler:375 Skipping 2 periods of 100ms.
tag(21) PLCTAG_EVENT_WRITE_COMPLETED
tag(22) created for WRITE: protocol=ab-eip&gateway=10.0.20.14&plc=controllogix&path=1,0&elem_size=4&elem_count=1&name=Test_tag[1]&debug=2
tag(22) calling plc_tag_write: protocol=ab-eip&gateway=10.0.20.14&plc=controllogix&path=1,0&elem_size=4&elem_count=1&name=Test_tag[1]&debug=2
tag(22) PLCTAG_EVENT_WRITE_STARTED
tag(22) PLCTAG_EVENT_WRITE_COMPLETED
tag(23) created for WRITE: protocol=ab-eip&gateway=10.0.20.14&plc=controllogix&path=1,0&elem_size=4&elem_count=1&name=Test_tag[1]&debug=2
tag(23) calling plc_tag_write: protocol=ab-eip&gateway=10.0.20.14&plc=controllogix&path=1,0&elem_size=4&elem_count=1&name=Test_tag[1]&debug=2
tag(23) PLCTAG_EVENT_WRITE_STARTED
2024-11-20 12:40:50.259 thread(1) tag(12) WARN plc_tag_generic_tickler:375 Skipping 2 periods of 100ms.
tag(23) PLCTAG_EVENT_WRITE_COMPLETED
tag(24) created for WRITE: protocol=ab-eip&gateway=10.0.20.14&plc=controllogix&path=1,0&elem_size=4&elem_count=1&name=Test_tag[1]&debug=2
tag(24) calling plc_tag_write: protocol=ab-eip&gateway=10.0.20.14&plc=controllogix&path=1,0&elem_size=4&elem_count=1&name=Test_tag[1]&debug=2
tag(24) PLCTAG_EVENT_WRITE_STARTED
2024-11-20 12:40:50.420 thread(1) tag(12) WARN plc_tag_generic_tickler:375 Skipping 2 periods of 100ms.
tag(24) PLCTAG_EVENT_WRITE_COMPLETED
2024-11-20 12:40:50.491 thread(2) tag(0) WARN lookup_tag:4439 Tag with ID 24 not found.
2024-11-20 12:40:50.491 thread(2) tag(0) WARN plc_tag_status:1785 Tag not found.
2024-11-20 12:40:50.491 thread(2) tag(0) WARN plc_tag_write:1941 Error PLCTAG_ERR_NOT_FOUND while trying to write tag!
2024-11-20 12:40:50.491 thread(2) tag(0) WARN lookup_tag:4439 Tag with ID 24 not found.
2024-11-20 12:40:50.491 thread(2) tag(0) WARN plc_tag_abort:1501 Tag not found.
Operation: Write, Error: PLC_ERR_NOT_FOUND, Source: TagWrite, Tag Path: protocol=ab-eip&gateway=10.0.20.14&plc=controllogix&path=1,0&elem_size=4&elem_count=1&name=Test_tag[1]&debug=2

As you can see, for tag 24, the tag gets created and then plc_tag_write called, but the call returns an error (that’s the line Operation: Write, Error: PLC_ERR_NOT_FOUND... . PLCTAG_EVENT_WRITE_STARTED and PLCTAG_EVENT_WRITE_COMPLETED are still called and, AFAIK, work as they should.

What I can’t figure out is why/how plc_tag_write fails here; the tag has just been successfully created (see pseudo-code above).

Any idea? Any info I can provide to help diagnose?

Currently it feels like plc_tag_destroy is called in my callback just before this line in plc_tag_write:, but then I’m not sure how I receive the PLCTAG_EVENT_WRITE_COMPLETED event before that 😉 https://github.com/libplctag/libplctag/blob/c55bc5876d938dda1c609750cde5ae4812d7b8a8/src/lib/lib.c#L1937

I would like to build a minimal test that reproduces the issue, but haven’t been successful yet. I’m currently testing the above theory by adding a small (1-2ms) delay before calling plc_tag_destroy in my callback.

Thanks a lot!

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (18 by maintainers)

Most upvoted comments

I am going to close this because #502 should fix it. This is a larger problem than you found!

The fact that the error occurs within one of the main API functions is why I am on the fence about this being a library bug. While the library cannot do everything to make app code correct, it should make it as easy as possible to do the right thing and not crash if something else is done.

I think I am going to call this a bug. There is a relatively simple fix for this. The code needs to get the status without looking up the ID. We already have a pointer to the memory for the tag and we have an incremented ref count so nothing is going to free it out from underneath us.

            /* get the tag status. */
            rc = plc_tag_status(id);

Needs to be something like:

critical_block(tag->api_mutex) {
        /* get the tag status. */
        if(tag->vtable && tag->vtable->status) {
            rc = tag->vtable->status(tag);
        } else {
            /* need a better error here */
            rc = PLCTAG_ERR_NOT_IMPLEMENTED;
        }
}

That skips the look up and uses the existing pointer.