-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
CommandLine.arguments
is Empty on Linux
#76080
Comments
@al45tair Seems like something's blowing away the "ABI" arguments list you added support for. |
This is a known downside of the approach we're using (which we adopted to work around a problem with Docker when using Rosetta). My guess is that OpenCV is either calling The workaround you mention will work with Glibc (and I think Bionic), but it will not work with Musl, because the latter doesn't pass The problem we have here is that we want this code to work with Musl, so we can't capture the arguments from a constructor, and we also want to avoid having to wait for a fix for the Rosetta issue that was breaking x86_64 Swift programs running in Docker containers on Apple Silicon machines. Once the latter is fixed, we might be able to do something else instead, but not until then. I'd recommend that you try to get OpenCV to not alter the environment from a global constructor/constructor function, or, if Musl compatibility isn't an issue for you, you could use your workaround above for now. I'm going to close this because there really isn't anything we can do; whatever choice we make, something is broken. |
Note: I'm not unsympathetic here; the problem is that we're boxed in by the decision to not capture arguments in the |
The problem appears to be something (and I don't think it's OpenCV itself, but rather something it pulls in) setting |
Looks like it's I filed open-mpi/hwloc#687 against OpenMPI's hwloc project to see if they will fix their code to not do this. |
Just to highlight the workaround, if you do
or
or similar, things should just work. It sounds like the |
Your insights were very helpful. Thank you! |
Description
On Linux,
CommandLine.arguments
sometimes returns an empty array. This issue occurs when certain libraries, such as OpenCV, are linked.In the implementation change of
argv
acquisition in #71885, there seems to be a problem. This implementation relies on theenviron
pointer to locateargv
. However, theenviron
pointer can change whensetenv
orputenv
is called. The process of locatingargv
byArgvGrabber
is executed in a C++ global variable constructor, but ifsetenv
is called before this, the process ofArgvGrabber
will fail. This issue depends on the initialization order of libraries at link time. When OpenCV is linked, its initialization happens before libswiftCore.so, which causes this problem.You can understand the initialization order by running the executable with
LD_DEBUG=libs
.Reproduction
Steps to reproduce the issue using OpenCV are summarized in the README of the following repository:
https://github.com/t-ae/empty-args-test
Even without using OpenCV, the following example reproduces this behavior intentionally:
https://github.com/t-ae/empty-args-test/tree/without_opencv
Expected behavior
Return an array containing at least one value.
For example,
[".build/debug/App"]
Environment
Additional information
Including the following C++ code in your project's source code can avoid the issue. However, this
method might not work in all execution environments.
The text was updated successfully, but these errors were encountered: