# Is it recommend to close TCPSocket after use?

**URL:** <https://discourse.julialang.org/t/is-it-recommend-to-close-tcpsocket-after-use/46189>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 7, 2020, 9:10am UTC](https://discourse.julialang.org/t/is-it-recommend-to-close-tcpsocket-after-use/46189 "2020-09-07T09:10:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [September 7, 2020, 9:10am UTC](https://discourse.julialang.org/t/is-it-recommend-to-close-tcpsocket-after-use/46189/1 "2020-09-07T09:10:12Z")

</div>

I’m using a `TCPSocket` but connect in a inner block

e.g.:

```julia
tcp = TCPSocket() # 1
if condition
    tcp = connect(1234)
end
...

```

I know that in C# one has to close and dispose streams and stuff. Is the same true for Julia. Do I need to close the `TCPSocket` from # 1?

I looked at the implementation an there is a `finalizer(uvfinalize, tcp)` defined. So does it close and dispose after it goes out of scope?

That’s the definition in Julia.

```julia
"""
    TCPSocket(; delay=true)

Open a TCP socket using libuv. If `delay` is true, libuv delays creation of the
socket's file descriptor till the first [`bind`](@ref) call. `TCPSocket` has various
fields to denote the state of the socket as well as its send/receive buffers.
"""
mutable struct TCPSocket <: LibuvStream
    handle::Ptr{Cvoid}
    status::Int
    buffer::IOBuffer
    cond::Base.ThreadSynchronizer
    readerror::Any
    sendbuf::Union{IOBuffer, Nothing}
    lock::ReentrantLock # advisory lock
    throttle::Int

    function TCPSocket(handle::Ptr{Cvoid}, status)
        tcp = new(
                handle,
                status,
                PipeBuffer(),
                Base.ThreadSynchronizer(),
                nothing,
                nothing,
                ReentrantLock(),
                Base.DEFAULT_READ_BUFFER_SZ)
        associate_julia_struct(tcp.handle, tcp)
        finalizer(uvfinalize, tcp)
        return tcp
    end
end

```
