Skip to content

Unloading of plctag.dll stuck in Shutdown #347

@Teldaril

Description

@Teldaril

Hello,

i have a problem with the shutdown of the library. Probably it is more related to the c-library than to the c#-wrapper. But currently i am working with the wrapper. Here is a sample to reproduce the behavior:

var tag = new Tag<IntPlcMapper, short>()
{
    Name = "Test",
    Gateway = "0.0.0.0",
    PlcType = PlcType.Slc500,
    Protocol = Protocol.ab_eip,
    Timeout = TimeSpan.FromSeconds(1),
    DebugLevel = DebugLevel.Spew
};

try
{
    tag.Read();
}
catch (Exception e)
{
    Console.WriteLine(e);
}

Thread.Sleep(1000);

//Everything works fine when this is uncommented
//LibPlcTag.Shutdown();

The problem happens when i rely on the shutdown when the plctag.dll is unloaded after leaving the Program.cs. On Windows 7 i can get a stuck process blocking the plctag.dll from further usage. (e.g. restarting the given sample)

When running the sample on Windows 7 and Windows 10 with the preemptive Shutdown() uncommented the log looks like this:

2023-07-18 09:25:14.959 thread(2) tag(0) SPEW cond_wait_impl:1125 Starting. Called from tag_tickler_func:634.
...
2023-07-18 09:25:15.059 thread(7) tag(0) INFO plc_tag_shutdown:1095 Starting.
...
2023-07-18 09:25:15.059 thread(7) tag(0) INFO lib_teardown:176 Tearing down library.
...
2023-07-18 09:25:15.059 thread(7) tag(0) INFO lib_teardown:181 Signaling tag tickler condition var.
2023-07-18 09:25:15.059 thread(7) tag(0) SPEW cond_signal_impl:1192 Starting.  Called from lib_teardown:182.
2023-07-18 09:25:15.059 thread(7) tag(0) SPEW cond_signal_impl:1208 Done for call at lib_teardown:182.
2023-07-18 09:25:15.059 thread(7) tag(0) INFO lib_teardown:186 Tearing down tag tickler thread.
2023-07-18 09:25:15.059 thread(2) tag(0) SPEW cond_wait_impl:1148 Condition var wait returned.
2023-07-18 09:25:15.059 thread(2) tag(0) SPEW cond_wait_impl:1171 Condition var signaled for call at tag_tickler_func:634.
2023-07-18 09:25:15.059 thread(2) tag(0) SPEW cond_wait_impl:1182 Done for call at tag_tickler_func:634.
...
2023-07-18 09:25:15.059 thread(2) tag(0) INFO tag_tickler_func:646 Terminating.
2023-07-18 09:25:15.059 thread(7) tag(0) INFO lib_teardown:193 Tearing down tag tickler condition var.
...
2023-07-18 09:25:15.059 thread(7) tag(0) INFO lib_teardown:212 Done.

The tag_tickler_func thread is signaled by cond_signal_impl and received by cond_wait_impl. The tag_tickler_func thread correctly terminates -> 2023-07-18 09:25:15.059 thread(2) tag(0) INFO tag_tickler_func:646 Terminating.

When running the sample on Windows 10 with the preemtive Shutdown() commented it looks like this:

2023-07-18 08:51:36.007 thread(2) tag(0) SPEW cond_wait_impl:1125 Starting. Called from tag_tickler_func:634.
...
2023-07-18 08:51:36.091 thread(6) tag(0) INFO plc_tag_shutdown:1095 Starting.
...
2023-07-18 08:51:36.091 thread(6) tag(0) INFO lib_teardown:176 Tearing down library.
...
2023-07-18 08:51:36.091 thread(6) tag(0) INFO lib_teardown:181 Signaling tag tickler condition var.
2023-07-18 08:51:36.091 thread(6) tag(0) SPEW cond_signal_impl:1192 Starting.  Called from lib_teardown:182.
2023-07-18 08:51:36.091 thread(6) tag(0) SPEW cond_signal_impl:1208 Done for call at lib_teardown:182.
2023-07-18 08:51:36.091 thread(6) tag(0) INFO lib_teardown:186 Tearing down tag tickler thread.
2023-07-18 08:51:36.091 thread(6) tag(0) INFO lib_teardown:193 Tearing down tag tickler condition var.
...
2023-07-18 08:51:36.091 thread(6) tag(0) INFO lib_teardown:212 Done.

The tag_tickler_func thread is signaled by cond_signal_impl but never responds in the log. No cond_wait_impl call and no termination. As if the thread is already gone. But Windows 10 just moves past cond_signal_impl and tears down the library.

When running the sample on Windows 7 with the preemtive Shutdown() commented it looks like this:

2023-07-18 08:51:36.007 thread(2) tag(0) SPEW cond_wait_impl:1125 Starting. Called from tag_tickler_func:634.
...
2023-07-18 08:51:36.091 thread(6) tag(0) INFO plc_tag_shutdown:1095 Starting.
...
2023-07-18 08:51:36.091 thread(6) tag(0) INFO lib_teardown:176 Tearing down library.
...
2023-07-18 08:51:36.091 thread(6) tag(0) INFO lib_teardown:181 Signaling tag tickler condition var.
2023-07-18 08:51:36.091 thread(6) tag(0) SPEW cond_signal_impl:1192 Starting.  Called from lib_teardown:182

The tag_tickler_func thread is signaled by cond_signal_impl but never responds in the log. But there the main thread is stuck.
I tried to analyze this with Process Explorer and got a single remaining thread stuck with a call stack:
Screenshot 2023-07-19 115331

My workaround is to preemtively call Shutdown().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions