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

Using on MacOS / iOS - semTrywait: invalid argument (Bad file descriptor) #218

Closed
epoberezkin opened this issue Jun 17, 2022 · 3 comments
Closed
Milestone

Comments

@epoberezkin
Copy link

Hello!

Is MacOS/iOS supported?

I have:

test = do
  sem <- semOpen "/test" OpenSemFlags {semCreate = True, semExclusive = False} stdFileMode 1
  semThreadWait sem
  semPost sem

It fails on semThreadWait:

uncaught exception: IOException of type InvalidArgument
semTrywait: invalid argument (Bad file descriptor)

#43 suggests it might not be working on MacOS?

Thank you!

@hasufell
Copy link
Member

hasufell commented Jul 10, 2022

We'll probably have to mark this function as unsupported operation like in other cases, such as

{-# WARNING semOpen
    "operation will throw 'IOError' \"unsupported operation\" (CPP guard: @#if HAVE_FDATASYNC@)" #-}

So you see this warning at compile time and at runtime simply get an io error.

But I'm not sure yet that this is really unsupported.

@hasufell
Copy link
Member

Can you try this patch:

--- a/System/Posix/Semaphore.hsc
+++ b/System/Posix/Semaphore.hsc
@@ -1,4 +1,5 @@
 {-# LANGUAGE Safe #-}
+{-# LANGUAGE CApiFFI #-}
 -----------------------------------------------------------------------------
 -- |
 -- Module      :  System.Posix.Semaphore
@@ -110,18 +111,18 @@ semGetValue_ sem ptr = do throwErrnoIfMinus1Retry_ "semGetValue" $
                           cint <- peek ptr
                           return $ fromEnum cint

-foreign import ccall safe "sem_open"
+foreign import capi safe "semaphore.h sem_open"
         sem_open :: CString -> CInt -> CMode -> CUInt -> IO (Ptr ())
-foreign import ccall safe "sem_close"
+foreign import capi safe "semaphore.h sem_close"
         sem_close :: Ptr () -> IO CInt
-foreign import ccall safe "sem_unlink"
+foreign import capi safe "semaphore.h sem_unlink"
         sem_unlink :: CString -> IO CInt

-foreign import ccall safe "sem_wait"
+foreign import capi safe "semaphore.h sem_wait"
         sem_wait :: Ptr () -> IO CInt
-foreign import ccall safe "sem_trywait"
+foreign import capi safe "semaphore.h sem_trywait"
         sem_trywait :: Ptr () -> IO CInt
-foreign import ccall safe "sem_post"
+foreign import capi safe "semaphore.h sem_post"
         sem_post :: Ptr () -> IO CInt
-foreign import ccall safe "sem_getvalue"
+foreign import capi safe "semaphore.h sem_getvalue"
         sem_getvalue :: Ptr () -> Ptr CInt -> IO Int

@hasufell
Copy link
Member

I can reproduce it and with my patch above, it seems to work.

So this appears to be another instance of: https://www.haskell.org/ghc/blog/20210709-capi-usage.html and #110 (comment)

@hasufell hasufell added this to the 2.8.0.0 milestone Jul 17, 2022
hasufell added a commit to hasufell/unix that referenced this issue Jul 17, 2022
haskell#218

Error was:

uncaught exception: IOException of type InvalidArgument
semTrywait: invalid argument (Bad file descriptor)
hasufell added a commit to hasufell/unix that referenced this issue Jul 17, 2022
haskell#218

Error was:

uncaught exception: IOException of type InvalidArgument
semTrywait: invalid argument (Bad file descriptor)
hasufell added a commit that referenced this issue Jul 17, 2022
* Add regression test for #218

#218

Error was:

uncaught exception: IOException of type InvalidArgument
semTrywait: invalid argument (Bad file descriptor)

* Fix 'semTrywait: invalid argument (Bad file descriptor)' wrt #218
hasufell added a commit to hasufell/unix that referenced this issue Jul 19, 2022
haskell#218

Error was:

uncaught exception: IOException of type InvalidArgument
semTrywait: invalid argument (Bad file descriptor)
hasufell added a commit that referenced this issue Jul 20, 2022
* Add regression test for #218

#218

Error was:

uncaught exception: IOException of type InvalidArgument
semTrywait: invalid argument (Bad file descriptor)

* Fix 'semTrywait: invalid argument (Bad file descriptor)' wrt #218

* Add regression test for putEnv wrt #68

* Synchronize ByteString and String modules

And fix free-bug in 'putEnv'. Fixes #68

* Add cabal.project

* Fix the error handling of posix_fallocate other than FreeBSD

This commit introduces a CPP guard to take care of the difference
between OSes of the errors of posix_fallocate.

On Linux and NetBSD, posix_fallocate reports error by returning an error
number. errno is not set.
On the other hand, on FreeBSD, posix_fallocate returns -1 and sets
errno.

The existing code could handle FreeBSD-style errors only.

* Remove obsolete FreeBSD-style error handling

The FreeBSD-style error (return -1, set errno) is obsolete as of
FreeBSD 11.0; it now reports errors as same as the other OSes.

* Future-proof glibc version check

* Resource: Fix warning in case of no RLIM_SAVED_{CUR,MAX}

This previously failed to build with a warning on FreeBSD 11.2

* Bump to 2.7.3

* Add Eq and Show instance to BaudRate

Needed for test suite

* Add CI

* Tighten lower base bound to only allow GHC-8.2+

Co-authored-by: matil019 <[email protected]>
Co-authored-by: Viktor Dukhovni <[email protected]>
Co-authored-by: Ben Gamari <[email protected]>
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

2 participants