• Alfredo Di Napoli's avatar
    ws: remove calls to recvMalloc · 15b732f5
    Alfredo Di Napoli authored
    Remove the calls to `recvMalloc` in favour of using the (patched) `recv`
    from the original nanomsg library, which shouldn't segfault anymore. The
    reason for using `recv` are a few, but mostly the fact that `recv` can
    allocated arbitrary-long payloads data (up to the 1MB limit) without an
    hardcoded limit like `recvMalloc` was imposing. Furthermore, `recv` does
    resource cleanup for us via `c_nn_freemsg`, whereas `recvMalloc` is not
    thread/exception safe. Consider the implementation:
    
    ```
    recvMalloc :: Receiver a => Socket a -> Int -> IO ByteString
    recvMalloc (Socket t sid) numBytes = do
      ptr <- mallocBytes numBytes
      -- receive by blocking the thread
      len <- c_nn_recv sid ptr (#const NN_MSG) 0 -- (#const NN_DONTWAIT)
      str <- C.packCStringLen (castPtr ptr, fromIntegral len)
      free ptr
      return str
    ```
    
    If any exception (synchronous or asynchronous) strikes _before_ the call
    to `free`, we would be leaking C memory.
    15b732f5
Main.hs 1.97 KB