diff --git a/Changelog.md b/Changelog.md
--- a/Changelog.md
+++ b/Changelog.md
@@ -1,3 +1,12 @@
+### 0.8.1.0
+
+- Function waitSetToLock from package *unix* calls C fcntl() unsafely which
+  caused hangs of the whole haskell RTS when it was waiting for lock release.
+  So this functions was reimplemented using *safe* semantics as
+  safeWaitToSetLock.
+- Exported function ngxExportReleaseLockedByteString was removed because Nginx
+  can call hs_free_stable_ptr() directly.
+
 ### 0.8.0.3
 
 - A better solution for ghc warnings set.
diff --git a/NgxExport.hs b/NgxExport.hs
--- a/NgxExport.hs
+++ b/NgxExport.hs
@@ -47,6 +47,8 @@
 import           Foreign.Marshal.Utils
 import           System.IO.Error
 import           System.Posix.IO
+import           System.Posix.Types
+import           System.Posix.Internals
 import           Control.Monad
 import           Control.Monad.Loops
 import           Control.Exception hiding (Handler)
@@ -479,8 +481,9 @@
     asyncIOCommon
     (do
         when (fstRun && fdlk /= -1) $ void $ iterateUntil (== True) $
-            (waitToSetLock fdlk (WriteLock, AbsoluteSeek, 0, 0) >> return True)
-            `catchIOError` (return . not . isEINTR)
+            (safeWaitToSetLock fdlk (WriteLock, AbsoluteSeek, 0, 0) >>
+                return True
+            ) `catchIOError` (return . not . isEINTR)
         x' <- B.unsafePackCStringLen (x, n)
         f x' fstRun
     ) fd efd
@@ -560,11 +563,36 @@
         pokeCStringLen t l p pl
         return 0
 
-foreign export ccall ngxExportReleaseLockedByteString ::
-    StablePtr L.ByteString -> IO ()
+{- SPLICE: safe version of waitToSetLock as defined in System.Posix.IO -}
 
-ngxExportReleaseLockedByteString :: StablePtr L.ByteString -> IO ()
-ngxExportReleaseLockedByteString = freeStablePtr
+foreign import ccall "HsBase.h fcntl"
+    safe_c_fcntl_lock :: CInt -> CInt -> Ptr CFLock -> IO CInt
+
+mode2Int :: SeekMode -> CShort
+mode2Int AbsoluteSeek = 0
+mode2Int RelativeSeek = 1
+mode2Int SeekFromEnd  = 2
+
+lockReq2Int :: LockRequest -> CShort
+lockReq2Int ReadLock  = 0
+lockReq2Int WriteLock = 1
+lockReq2Int Unlock    = 2
+
+allocaLock :: FileLock -> (Ptr CFLock -> IO a) -> IO a
+allocaLock (lockreq, mode, start, len) io =
+    allocaBytes 32 $ \p -> do
+        (`pokeByteOff`  0) p (lockReq2Int lockreq)
+        (`pokeByteOff`  2) p (mode2Int mode)
+        (`pokeByteOff`  8) p start
+        (`pokeByteOff` 16) p len
+        io p
+
+safeWaitToSetLock :: Fd -> FileLock -> IO ()
+safeWaitToSetLock (Fd fd) lock = allocaLock lock $
+    \p_flock -> throwErrnoIfMinus1_ "safeWaitToSetLock" $
+        safe_c_fcntl_lock fd 7 p_flock
+
+{- SPLICE: END -}
 
 foreign export ccall ngxExportVersion :: Ptr CInt -> CInt -> IO CInt
 
diff --git a/ngx-export.cabal b/ngx-export.cabal
--- a/ngx-export.cabal
+++ b/ngx-export.cabal
@@ -1,5 +1,5 @@
 name:                       ngx-export
-version:                    0.8.0.7
+version:                    0.8.1.0
 synopsis:                   Helper module for Nginx haskell module
 description:                Helper module for
         <http://github.com/lyokha/nginx-haskell-module Nginx haskell module>
