Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DicomServer does not close TCP connection #795

Closed
mia-san opened this issue Feb 13, 2019 · 3 comments · Fixed by #802
Closed

DicomServer does not close TCP connection #795

mia-san opened this issue Feb 13, 2019 · 3 comments · Fixed by #802

Comments

@mia-san
Copy link
Contributor

mia-san commented Feb 13, 2019

reported by @cjcaldwell via gitter

Expected behavior

DicomServer closes TCP connection after client closed connection.

Actual behavior

DicomServer leaves TCP connection open after client closed connection.

Steps to reproduce the behavior

send C-ECHO request repeatedly to DicomServer and observe sockets.
see additional comments.

fo-dicom version and OS/platform

4.0.0

thank you again to @cjcaldwell

@mia-san
Copy link
Contributor Author

mia-san commented Feb 13, 2019

//
//  sample C-ECHO SCP
//
using Dicom.Log;
using Dicom.Network;
using System;

namespace ConsoleApp1
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            LogManager.SetImplementation(ConsoleLogManager.Instance);

            using (var server = new DicomServer<DicomCEchoProvider>())
            {
                server.StartAsync(null, 12345, null, null, null, null)
                    .GetAwaiter()
                    .GetResult();
            }

            Console.ReadLine();
        }
    }
}

@mia-san
Copy link
Contributor Author

mia-san commented Feb 13, 2019

REM
REM request C-ECHO repeatedly with dcmtk echoscu.exe
REM

SET ECHOSCU_PROGRAM=echoscu.exe
SET ECHOSCU_HOST=127.0.0.1
SET ECHOSCU_PORT=12345
SET ECHOSCU_CALLING=ANY-SCU
SET ECHOSCU_CALLED=ANY-SCP

FOR /L %%c IN (1, 1, 100) DO %ECHOSCU_PROGRAM% --aetitle %ECHOSCU_CALLING% --call %ECHOSCU_CALLED% %ECHOSCU_HOST% %ECHOSCU_PORT%

netstat -na | find "%ECHOSCU_PORT%"

pause

@mia-san
Copy link
Contributor Author

mia-san commented Feb 13, 2019

I found that just letting NetworkStream to keep reference to socket or TcpClient is not enough.

DicomServer takes DicomService-derived class as type parameter.

public class DicomServer<T> : IDicomServer<T> where T : DicomService, IDicomServiceProvider

DicomService-derived class is instantiated by CreateScp.

var instance = (T)Activator.CreateInstance(typeof(T), stream, _fallbackEncoding, Logger);

scp is assigned to local variable

var scp = CreateScp(networkStream);

scp's RunAsync is invoked, and scp variable is just forgotten.

_services.Add(scp.RunAsync());

this means that DicomService-derived class would only be disposed via finalizer.

we have to make DicomServer to dispose instance of class T as well.

gofal added a commit to mia-san/fo-dicom that referenced this issue Mar 5, 2019
gofal added a commit to mia-san/fo-dicom that referenced this issue Mar 8, 2019
gofal added a commit that referenced this issue Mar 12, 2019
DicomServer does not close TcpClient/socket after client closed connection.
@gofal gofal reopened this Mar 14, 2019
@gofal gofal closed this as completed Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants