.net 诊断Linux Docker容器中本机库的“DllNotFoundException”

pb3s4cty  于 2023-02-06  发布在  .NET
关注(0)|答案(1)|浏览(258)

我有一个依赖于librdkafka-redist nuget的dotnet应用程序,我想在一个基于alpine映像的docker容器中运行这个应用程序,这个容器是为linux-arm 64平台构建的。
在运行时,我看到:

Unhandled exception. System.DllNotFoundException: Failed to load the librdkafka native library.
   at Confluent.Kafka.Impl.Librdkafka.TrySetDelegates(List`1 nativeMethodCandidateTypes)
   at Confluent.Kafka.Impl.Librdkafka.LoadLinuxDelegates(String userSpecifiedPath)
   at Confluent.Kafka.Impl.Librdkafka.Initialize(String userSpecifiedPath)
   at Confluent.Kafka.Producer`2..ctor(ProducerBuilder`2 builder)
   at Confluent.Kafka.ProducerBuilder`2.Build()

库似乎已正确打包;如果我将shell装入容器,我会看到以下内容:

$ ls /app/runtimes/linux-arm64/native
librdkafka.so

但我也知道我会遇到问题,因为alpine没有附带glibc支持:

$ ldd /app/runtimes/linux-arm64/native/librdkafka.so
    /lib/ld-musl-aarch64.so.1 (0xffffb4158000)
    libm.so.6 => /lib/ld-musl-aarch64.so.1 (0xffffb4158000)
    libdl.so.2 => /lib/ld-musl-aarch64.so.1 (0xffffb4158000)
    libpthread.so.0 => /lib/ld-musl-aarch64.so.1 (0xffffb4158000)
    libc.so.6 => /lib/ld-musl-aarch64.so.1 (0xffffb4158000)
Error loading shared library ld-linux-aarch64.so.1: No such file or directory (needed by /app/runtimes/linux-arm64/native/librdkafka.so)
Error relocating /app/runtimes/linux-arm64/native/librdkafka.so: __vsnprintf_chk: symbol not found
...

我可以使用alpine gcompat包和patchelf来解决其中的一些问题(稍微匿名的Dockerfile):

FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine

RUN apk add patchelf
RUN apk add binutils
RUN apk add gcompat

RUN    adduser --disabled-password \
        --gecos "" \
        --no-create-home \
        --uid 10028 \
        myuser

USER myuser

COPY --chown=myuser:myuser . app/

RUN patchelf --remove-needed ld-linux-aarch64.so.1 /app/runtimes/linux-arm64/native/librdkafka.so && \
    patchelf --add-needed libgcompat.so.0 /app/runtimes/linux-arm64/native/librdkafka.so

ENV COREHOST_TRACE=1

ENTRYPOINT [ "dotnet", "app/MyApp.dll" ]

在这一点上我 * 认为 * 我已经解决了我的原生依赖问题:

$ ldd /app/runtimes/linux-arm64/native/librdkafka.so
    /lib/ld-musl-aarch64.so.1 (0xffffb5290000)
    libgcompat.so.0 => /lib/libgcompat.so.0 (0xffffb4d5b000)
    libm.so.6 => /lib/ld-musl-aarch64.so.1 (0xffffb5290000)
    libdl.so.2 => /lib/ld-musl-aarch64.so.1 (0xffffb5290000)
    libpthread.so.0 => /lib/ld-musl-aarch64.so.1 (0xffffb5290000)
    libc.so.6 => /lib/ld-musl-aarch64.so.1 (0xffffb5290000)
    libucontext.so.1 => /lib/libucontext.so.1 (0xffffb4d49000)
    libobstack.so.1 => /usr/lib/libobstack.so.1 (0xffffb4d36000)

但是我仍然在运行时得到DllNotFoundException。当我设置COREHOST_TRACE=1时,我可以看到

Adding runtimeTargets native asset runtimes/linux-arm64/native/librdkafka.so rid=linux-arm64 assemblyVersion= fileVersion=0.0.0.0 from librdkafka.redist/1.9.2
...
Chose linux-arm64, so removing rid (win-x86) specific assets for package librdkafka.redist/1.9.2 and asset type native
Chose linux-arm64, so removing rid (win-x64) specific assets for package librdkafka.redist/1.9.2 and asset type native
Chose linux-arm64, so removing rid (osx-x64) specific assets for package librdkafka.redist/1.9.2 and asset type native
Chose linux-arm64, so removing rid (osx-arm64) specific assets for package librdkafka.redist/1.9.2 and asset type native
Chose linux-arm64, so removing rid (linux-x64) specific assets for package librdkafka.redist/1.9.2 and asset type native
...
Reconciling library librdkafka.redist/1.9.2
Parsed native deps entry 0 for asset name: librdkafka from package: librdkafka.redist, library version: 1.9.2, relpath: runtimes/linux-arm64/native/librdkafka.so, assemblyVersion , fileVersion 0.0.0.0
...
Processing native/culture for deps entry [librdkafka.redist, 1.9.2, runtimes/linux-arm64/native/librdkafka.so]
  Considering entry [librdkafka.redist/1.9.2/runtimes/linux-arm64/native/librdkafka.so], probe dir [], probe fx level:0, entry fx level:0
    Relative path query /app/runtimes/linux-arm64/native/librdkafka.so (skipped file existence check)
    Probed deps dir and matched '/app/runtimes/linux-arm64/native/librdkafka.so'
Adding to native path: /app/runtimes/linux-arm64/native/
...
Property NATIVE_DLL_SEARCH_DIRECTORIES = /app/runtimes/linux-arm64/native/:/usr/share/dotnet/shared/Microsoft.NETCore.App/6.0.11/:

在它启动失败之前。
所以我想我已经a)确定框架知道(并希望尝试)加载我认为它是的本机库,b)确定没有丢失的依赖项,这将意味着库不能加载。
我是否可以通过更多的过程或步骤来进一步诊断发生了什么?我 * 认为 * 有一个ldd没有向我显示的series of dependencies-例如,在opensslzlib等上,这可能是问题所在?

mklgxw1f

mklgxw1f1#

请尝试更新您的Kafka版本。我们在1.7.0版本上运行了一年,突然出现了这个错误。我们更新到2.0.2版本,错误消失了。

相关问题