You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Cabal构建项目时遭遇ghc-pkg hLock资源耗尽错误求助

解决Cabal编译时hLock: resource exhausted (No locks available)错误

问题描述

仅执行cabal init创建默认Haskell项目,运行cabal build时触发错误:

hLock: resource exhausted (No locks available)

已尝试更新Cabal、GHC,查阅锁文件相关资料并搜索该错误,未找到有效解决方法。以下是cabal build -v的详细输出:

/homeLocal/mvillagra/.ghcup/bin/ghc --print-global-package-db
Reading available packages of hackage.haskell.org...
Using most recent state specified from most recent cabal update
index-state(hackage.haskell.org) = 2023-07-04T18:27:42Z
/usr/bin/pkg-config --version
/usr/bin/pkg-config --variable pc_path pkg-config
/usr/bin/pkg-config --version
/usr/bin/pkg-config --list-all
/usr/bin/pkg-config --modversion sqlite3 Qt5DBus ompi-fort pixman-1 gmodule-export-2.0 cairo-svg libdrm_intel recordproto iso-codes nettle gnutls eigen3 libpcreposix lapack-netlib atk-bridge-2.0 ompi-c spatialite cairo-tee dri dbus-1 mobile-broadband-provider-info xcb-render libzmq fftw3 libpcre2-16 python-3.5m xkbcommon systemd adwaita-icon-theme libbsd cairo-png wayland-cursor spice-controller libcurl pangoxft xcursor xft libidn glu 'gtk+-x11-3.0' mircookie xtrans gdk-pixbuf-2.0 xf86vidmodeproto lapack netcdf damageproto proj Qt5Sql kbproto expat cairo-xlib mit-krb5-gssapi libavformat uuid mpi libpcre2-8 mpi-c fontutil libpcre2-32 xcb-dri2 xcb-dri3 panel randrproto libxdot libdap xrandr wayland-egl bash-completion gdk-2.0 spice-client-gtk-2.0 x264 scrnsaverproto xcomposite libtasn1 dri2proto icu-i18n sm xorg-sgml-doctools libswresample-ffmpeg cairo-ps p11-kit-1 libcdt spice-client-glib-2.0 python gdk-mir-3.0 protobuf-lite python2 mirclient python3 gmodule-2.0 gdk-pixbuf-xlib-2.0 libavcodec lapack-atlas libdrm_radeon libdrm theora fixesproto pango libsodium pangoft2 atspi-2 Qt5XcbQpa xineramaproto gdk-3.0 compositeproto icu-uc libbsd-ctor Qt5PrintSupport Qt5Test ompi-cxx libpcre libpcre16 cairo-xcb-shm glproto cairo-pdf pangocairo jsoncpp Qt5EglDeviceIntegration Qt5Core x11-xcb libpq gthread-2.0 xcb-glx 'gtk+-broadway-3.0' vdpau libssl libpcrecpp cairo-xcb menu gl xorg-wacom libcrypto dlib-1 egl libxml-2.0 mit-krb5 libdrm_nouveau libpathplan yelp-xsl xextproto cairo-gobject wayland-client gmodule-no-export-2.0 libv4l2rds libdrm_amdgpu x11 libgvpr xtst 'gtk+-mir-3.0' libpcre32 gsl 'gtk+-unix-print-2.0' gdk-broadway-3.0 hogweed mysqlclient poppler-data wayland-server xi udev Qt5Xml hwloc libR libarchive xbitmaps xt cairo-fc cairo glib-2.0 libdapclient gdk-wayland-3.0 Qt5Network libswscale-ffmpeg cairo-ft icu-io xcb-sync gio-unix-2.0 libswresample glesv2 libavutil-ffmpeg xfixes libv4lconvert mpi-cxx xcb-xfixes hdf5 theoraenc 'gtk+-unix-print-3.0' tic mpich xinerama epoxy libbsd-overlay tinfo hdf5-serial libpng libpcre2-posix libwebpdemux blas-netlib 'gtk+-wayland-3.0' libccgnu2 gdk-x11-2.0 usbutils htslib xerces-c xshmfence xcb-randr Qt5Gui freetype2 xext libjpeg libtiff-4 xau fftw3f xcb-present ice xdamage xkbcomp fftw3l openssl atk xxf86vm libwebpmux fftw3q python-2.7 harfbuzz icu-le libffi wayland-scanner 'gtk+-2.0' cairo-script xcb-shape xkeyboard-config harfbuzz-icu libavcodec-ffmpeg pthread-stubs shared-mime-info xrender xproto inputproto icu-lx xcb-shm libpng12 xscrnsaver libccext2 orte protobuf xcb mircore Qt5OpenGL liblzma libswscale gdk-x11-3.0 libavutil ogg gdal spice-protocol libwebp ncurses form fontconfig ompi cairo-xlib-xrender 'gtk+-3.0' harfbuzz-gobject mpi-fort Qt5OpenGLExtensions zlib python-3.5 com_err Qt5Widgets 'ncurses++' cfitsio blas-atlas gobject-2.0 Qt5Concurrent libdapserver gio-2.0 theoradec libcgraph libgvc xdmcp blas libv4l1 libv4l2 'gtk+-x11-2.0' libavformat-ffmpeg renderproto
Resolving dependencies...
Component graph for test-0.1.0.0: component exe:test
component test-0.1.0.0-inplace-test include base-4.14.3.0
unit test-0.1.0.0-inplace-test include base-4.14.3.0
Build profile: -w ghc-8.10.7 -O1
In order, the following will be built:
 - test-0.1.0.0 (exe:test) (first run)
creating /home/mvillagra/code/test/dist-newstyle/build
creating /home/mvillagra/code/test/dist-newstyle/tmp
creating
/home/mvillagra/code/test/dist-newstyle/build/x86_64-linux/ghc-8.10.7/test-0.1.0.0/x/test
creating
/home/mvillagra/code/test/dist-newstyle/build/x86_64-linux/ghc-8.10.7/test-0.1.0.0/x/test/cache
Using self-exec internal setup method with build-type Simple and args:
["act-as-setup","--build-type=Simple","--","build","--verbose=2","--builddir=/home/mvillagra/code/test/dist-newstyle/build/x86_64-linux/ghc-8.10.7/test-0.1.0.0/x/test"]
/homeLocal/mvillagra/.ghcup/bin/cabal-3.8.1.0 act-as-setup --build-type=Simple
-- build --verbose=2
--builddir=/home/mvillagra/code/test/dist-newstyle/build/x86_64-linux/ghc-8.10.7/test-0.1.0.0/x/test
Component build order: executable 'test'
/homeLocal/mvillagra/.ghcup/ghc/8.10.7/bin/ghc-pkg-8.10.7 init /home/mvillagra/code/test/dist-newstyle/build/x86_64-linux/ghc-8.10.7/test-0.1.0.0/x/test/package.conf.inplace
ghc-pkg: {handle: /home/mvillagra/code/test/dist-newstyle/build/x86_64-linux/ghc-8.10.7/test-0.1.0.0/x/test/package.conf.inplace/package.cache.lock}: hLock: resource exhausted (No locks available)

解决方案

1. 调整系统文件锁限制

该错误核心原因是系统允许的文件句柄/锁资源耗尽。

  • 临时生效:执行ulimit -n 4096,随后重新运行cabal build
  • 永久生效:编辑/etc/security/limits.conf,添加:
    * soft nofile 4096
    * hard nofile 8192
    
    保存后重启终端或重新登录

2. 清理Cabal构建缓存

删除项目下的dist-newstyle目录以清除残留锁文件:

rm -rf dist-newstyle

重新执行cabal build

3. 终止残留进程

检查并关闭所有闲置的GHC/Cabal进程:

ps aux | grep -E '(ghc|cabal)' | grep -v grep | awk '{print $2}' | xargs kill -9

之后重新构建

4. 切换GHC/Cabal版本

尝试使用兼容性更稳定的版本组合:

# 安装并切换到指定版本
ghcup install ghc 9.0.2
ghcup install cabal 3.6.2.0
ghcup set ghc 9.0.2
ghcup set cabal 3.6.2.0

重新初始化项目并执行构建

5. 修复目录权限

确保项目目录及Cabal相关缓存目录拥有读写权限:

chmod -R 755 ~/.cabal ~/.ghcup
chmod -R 755 /home/mvillagra/code/test

内容的提问来源于Stack Exchange,提问作者Marcos Villagra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 01:59:51