Crash on shutdown of DcmQueryRetrieveSCP

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
nbeck
Posts: 12
Joined: Fri, 2022-10-28, 12:56

Crash on shutdown of DcmQueryRetrieveSCP

#1 Post by nbeck »

Hello!

I am trying to set up a test environment for my application's DICOM communication. To this end, I start a thread that runs an instance of DcmQueryRetrieveSCP. At the end of all test runs this thread needs be terminated, which I am trying to do with the private shutdown SOP. However, this causes a crash in DcmQueryRetrieveSCP.

I looked at the issue in the debugger and found the following: DcmQueryRetrieveSCP::waitForAssociation() calls negotiateAssociation(), which detects the shutdown SOP and calls refuseAssocitaion:

Code: Select all

    /*
     * check if we have negotiated the private "shutdown" SOP Class
     */
    if (0 != ASC_findAcceptedPresentationContextID(assoc, UID_PrivateShutdownSOPClass))
    {
      DCMQRDB_INFO("Shutting down server ... (negotiated private \"shut down\" SOP class)");
      refuseAssociation(&assoc, CTN_NoReason);
      return ASC_SHUTDOWNAPPLICATION;
    }
Then, refuseAssociation() rejects, dropd and destroys the association:

Code: Select all

    cond = ASC_rejectAssociation(*assoc, &rej);

    if (cond.bad())
    {
      DCMQRDB_ERROR("Association Reject Failed: " << DimseCondition::dump(temp_str, cond));
    }

    cond = ASC_dropAssociation(*assoc);
    if (cond.bad())
    {
      DCMQRDB_ERROR("Cannot Drop Association: " << DimseCondition::dump(temp_str, cond));
    }
    cond = ASC_destroyAssociation(assoc);
    if (cond.bad())
    {
      DCMQRDB_ERROR("Cannot Destroy Association: " << DimseCondition::dump(temp_str, cond));
    }
When the programm returns to DcmQueryRetrieveSCP::waitForAssociation(), it goes to the clean up section, where it tries to drop the association again:

Code: Select all

    // clean-up association
    OFCondition oldcond = cond;    /* store condition flag for later use */
    cond = ASC_dropAssociation(assoc);
    if (cond.bad())
    {
        DCMQRDB_ERROR("Cannot Drop Association: " << DimseCondition::dump(temp_str, cond));
    }
    cond = ASC_destroyAssociation(&assoc);
    if (cond.bad())
    {
        DCMQRDB_ERROR("Cannot Destroy Association: " << DimseCondition::dump(temp_str, cond));
    }
This crashes the application. Is there a way to avoid this? I have already set options.allowShutdow_ to true.

Any help would be appreciated!

J. Riesmeier
DCMTK Developer
Posts: 2506
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Crash on shutdown of DcmQueryRetrieveSCP

#2 Post by J. Riesmeier »

Thank you for your report. I think the problem has been introduced with commit c34f4e46e6.

Here is a possible patch to fix this:

Code: Select all

diff --git a/dcmqrdb/libsrc/dcmqrsrv.cc b/dcmqrdb/libsrc/dcmqrsrv.cc
index d9534238e..4c450c284 100644
--- a/dcmqrdb/libsrc/dcmqrsrv.cc
+++ b/dcmqrdb/libsrc/dcmqrsrv.cc
@@ -1140,20 +1140,22 @@ OFCondition DcmQueryRetrieveSCP::waitForAssociation(T_ASC_Network * theNet)
 #endif
     }
 
-    // clean-up association
-    OFCondition oldcond = cond;    /* store condition flag for later use */
-    cond = ASC_dropAssociation(assoc);
-    if (cond.bad())
-    {
-        DCMQRDB_ERROR("Cannot Drop Association: " << DimseCondition::dump(temp_str, cond));
-    }
-    cond = ASC_destroyAssociation(&assoc);
-    if (cond.bad())
+    // avoid double deletion of memory
+    if (cond != ASC_SHUTDOWNAPPLICATION)
     {
-        DCMQRDB_ERROR("Cannot Destroy Association: " << DimseCondition::dump(temp_str, cond));
+      // clean-up association
+      cond = ASC_dropAssociation(assoc);
+      if (cond.bad())
+      {
+          DCMQRDB_ERROR("Cannot Drop Association: " << DimseCondition::dump(temp_str, cond));
+      }
+      cond = ASC_destroyAssociation(&assoc);
+      if (cond.bad())
+      {
+          DCMQRDB_ERROR("Cannot Destroy Association: " << DimseCondition::dump(temp_str, cond));
+      }
     }
 
-    if (oldcond == ASC_SHUTDOWNAPPLICATION) cond = oldcond; /* abort flag is reported to top-level wait loop */
     return cond;
 }
It would be nice if you could check this and give us feedback.

nbeck
Posts: 12
Joined: Fri, 2022-10-28, 12:56

Re: Crash on shutdown of DcmQueryRetrieveSCP

#3 Post by nbeck »

Thanks for the prompt reaction. Your patch fixes the problem for me.

J. Riesmeier
DCMTK Developer
Posts: 2506
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Crash on shutdown of DcmQueryRetrieveSCP

#4 Post by J. Riesmeier »

Thank you for the feedback. We will do some more testing and then commit the required changes.

Post Reply

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 1 guest