Skip to content
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

Limited support for stand-alone clients on Linux/macOS #8

Open
john-hen opened this issue Feb 7, 2021 · 2 comments
Open

Limited support for stand-alone clients on Linux/macOS #8

john-hen opened this issue Feb 7, 2021 · 2 comments
Labels
can't fix Issue cannot be resolved.

Comments

@john-hen
Copy link
Collaborator

john-hen commented Feb 7, 2021

MPh 0.8.0, released today, adds support for Linux. That support is slightly limited in that it does not fully work out of the box. In order to start a stand-alone client (as opposed to running the Comsol session in client-server mode), it requires the user take an extra step and configure the search path for shared libraries as described in the documentation.

This is fine, but not ideal. For one thing, it's not just pip install and be done with it. And for another, it prevents users from easily selecting a specific Comsol version as the computation back-end, which is sometimes useful when reproducing earlier simulation results. So ideally, the manual configuration would not be necessary, but instead be done from Python code.

To perhaps track progress on this, or discuss a possible solution, here are some additional details. MPh now follows the instructions exactly as given in Comsol's Programming Manual (specifically, on pages 23 and 916) for running Java applications from the Eclipse IDE. That is, it sets up the environment in that same way before "initializing" the "stand-alone" client. (As opposed to a client-server connection, which works anyway, without any additional configuration.)

However, that alone does not do it. While all appears to be in working order as far as the Java Virtual Machine is concerned, in that it is aware of the locations of the external (native) libraries, these libraries themselves, which are dynamically loaded, have trouble finding each other, leading to a java.lang.UnsatisfiedLinkError. Which is odd: One would think that these child processes inherit the environment set up in the parent process.

To showcase all of this, I have added the demo script test_Linux to the sandbox branch.

@john-hen
Copy link
Collaborator Author

john-hen commented Feb 14, 2021

So as it turns out, this is a fundamental issue with Unix-like operating systems — Linux same as macOS. I don't think it will ever be resolved in the way I was hoping.

The problem starts with the fact that relying on LD_LIBRARY_PATH is bad and was originally meant as a handy tool for developers, not a means to deploying applications to end users. And yet many software companies do just that, to have their executables find their very own shared libraries, even though there are better ways to accomplish the same thing.

Comsol is just one example of many. Such as Firefox and Chrome/Chromium were, until they fixed that. Whereas on Windows the Comsol executable is a binary file, comsol.exe, on Linux it's a shell script, hundreds of lines long, that, among many other things, sets LD_LIBRARY_PATH, only to then start the actual application. This is a common work-around if you don't include custom search paths in the binaries at build time.

The problem is exacerbated by the fact that the system function that loads external shared libraries on Linux, dlopen(), only ever checks LD_LIBRARY_PATH once when a process starts, but then never looks back. So the loader won't notice any changes to that environment variable by the time Python is running. Changing it has no effect on the current process.

This explains why the script test_Linux fails to run. Unless, of course, we export LD_LIBRARY_PATH in the shell before starting the script, or, alternatively, run the script as a subprocess after setting os.environ['LD_LIBRARY_PATH'] in a Python wrapper. Neither one of which is an out-of-the-box solution.

Commonly suggested work-arounds are re-starting the original Python process via os.execv() (which… just no) or trying to preload any and all .so files up front via ctypes.CDLL() (which fails here because they are all interdependent).

Microsoft, for once, got this right. On Windows, the library loader does heed live changes to the search path, which is just PATH in that case.

@john-hen john-hen added the bug Something isn't working. label Feb 15, 2021
john-hen added a commit that referenced this issue Feb 24, 2021
As detailed in issue #8, setting the environment variable
for the search path of shared libraries has no effect on
the current process on Unix-like systems. And on Windows
is not even needed. The code that does this was therefore
removed. But a check was added for Linux and macOS that tests
if the environment variable has been correctly set by the user
beforehand if, and only if, a stand-alone client is to be
initialized. Updated the documentation accordingly.
john-hen added a commit that referenced this issue Feb 24, 2021
As detailed in issue #8, starting a stand-alone client does
not work out of the box on Unix-like operating systems.
Relying on the client-server mode instead seems to be the
way forward. The test suite now uses that mode by default.
Stand-alone clients can still be tested by passing the
command-line argument "stand-alone" to a test script.
@john-hen john-hen changed the title Linux support Stand-alone clients on Linux Feb 24, 2021
@john-hen john-hen added can't fix Issue cannot be resolved. and removed bug Something isn't working. labels Feb 24, 2021
@john-hen john-hen changed the title Stand-alone clients on Linux Limited support for stand-alone clients on Linux Feb 24, 2021
@john-hen john-hen mentioned this issue Mar 10, 2021
Closed
@john-hen
Copy link
Collaborator Author

john-hen commented Mar 10, 2021

MPh 0.9.0, released today, provides the new convenience function mph.start() which works around the issue described here. On Windows, it starts a stand-alone client. On Linux and macOS, it uses client–server mode to create the local session. More details in the documentation chapter "Limitations".

@john-hen john-hen reopened this Oct 7, 2021
@john-hen john-hen changed the title Limited support for stand-alone clients on Linux Limited support for stand-alone clients on Linux/macOS Oct 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't fix Issue cannot be resolved.
Projects
None yet
Development

No branches or pull requests

1 participant