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

Remove 'Closed' state from linear network API #3252

Open
2 tasks done
Alex1005a opened this issue Apr 6, 2024 · 0 comments
Open
2 tasks done

Remove 'Closed' state from linear network API #3252

Alex1005a opened this issue Apr 6, 2024 · 0 comments

Comments

@Alex1005a
Copy link
Contributor

Alex1005a commented Apr 6, 2024

  • I have read CONTRIBUTING.md.
  • I have checked that there is no existing PR/issue about my proposal.

Motivation

Currently, using the linear network API, you can close the same socket many times. This could be a problem because a new socket could be created with the same descriptor and we could affect how it works.

The program below will output "Error 2: errno 9" from the problem described. Return code 9 means "Bad file descriptor". This error occurs when using a socket that is already closed.

example2 : LinearIO io => (1 sock : Socket Ready) -> L io ()
example2 sock = do
    sleep 2
    (Nothing # sock) <- bind sock Nothing 1111
        | (Just err # sock) => do
            printLn "Error 3: errno \{show err}"
            done sock
    sleep 3
    (Nothing # sock) <- listen sock
        | (Just err # sock) => do
            printLn "Error 2: errno \{show err}"
            done sock
    close sock >>= done

example1 : LinearIO io => (1 sock : Socket Ready) -> L io ()
example1 sock = do
    (Nothing # sock) <- bind sock Nothing 1111
        | (Just err # sock) => do
            printLn "Error 1: errno \{show err}"
            done sock
    sock <- close sock
    sleep 4
    close sock >>= done


main : IO ()
main = do
    th1 <- fork $ run $ newSocket AF_INET Stream 0
        example1
        (\err => pure ())
    th2 <- fork $ run $ newSocket AF_INET Stream 0
        example2
        (\err => pure ())
    threadWait th1
    threadWait th2
    pure ()

The proposal

In Control.Linear.Network:

  1. Remove the Closed state
  2. Remove done function
  3. Сhange close function and all functions returning Socket Closed

The result will be a similar API as in the article Linear Haskell and ATS.

Alternatives considered

You can impose a constraint on the state of the socket in the close function as in this Haskell library. This fixes the problem, but it's more complicated and makes less sense since there's hardly any use for a closed socket handle.

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

No branches or pull requests

1 participant