Skip to content

Sockets don't have finalizers #4

Description

@bos

Yow! I just noticed this. If you create a Socket, it stays open forever even if no longer reachable.

Activity

  1. bos commented on Feb 2, 2011

    @bos
    ContributorAuthor

    I took a quick look at fixing this, and it's not completely trivial.

    The easiest thing, I thought, would be to use addMVarFinalizer to associate a finalizer with the MVar we already use to track the socket state. But the finalizer would want to know the state of the socket in order to close it safely, and if the finalizer has a reference to the MVar to read its state, I think that will unintentionally keep the MVar alive.

    We cannot simply unconditionally c_close the file descriptor, because by the time the finalizer is called on a closed Socket, the fd will probably refer to a different file entirely.

    So. Instead, I'm adding a finalizer to the 'Socket' itself, using 'System.Mem.Weak'.

  2. lpeterse commented on May 18, 2015

    @lpeterse

    Hi Bryan,

    I'm currently trying to get an overview of the state of the network package.

    What is the state of this issue? I grepped the package for 'Weak' without result and therefore assume that whatever you did, did not make it into master.

  3. lpeterse commented on Aug 11, 2016

    @lpeterse

    For anyone stumbling upon this issue: The finalization mechanism proposed by @bos does work when using mkWeakMVar (instead of now obsolete addMVarFinalizer). The considerations with the finalizer itself keeping the MVar alive do not apply.

    Here is a minimal example: lpeterse/haskell-socket#20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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