Skip to content

find agent_pid even when using a PEP397 launcher - #24905

Closed
mattip wants to merge 2 commits into
ray-project:masterfrom
mattip:issue24361
Closed

mattip wants to merge 2 commits into
ray-project:masterfrom
mattip:issue24361

Conversation

@mattip

@mattip mattip commented May 18, 2022 •

Copy link
Copy Markdown
Contributor

Why are these changes needed?

When using ray inside a virtualenv on windows, python.exe as reported by sys.executable is a PEP397 launcher to the actual python as reported by os.getpid():

>>> import sys, os, psutil
>>> >>> print(sys.executable)
C:\temp\issue24361\Scripts\python.exe
>>> os.getpid()
2208
>>> child = psutil.Process(2208)
>>> child.cmdline()
['C:\\oss\\CPython38\\python.exe']
>>> child.parent().cmdline()
['C:\\temp\\issue24361\\Scripts\\python.exe']
>>> child.parent().pid
6424

When the agent_manager launches the agent process via Process::Process(), it gets the PID of the launcher process (6424), which is what is expected as an ID when registering the agent in the gRPC callback. But inside agent.py, the child process reports the PID via os.getpid(), which is 2208, and this is the wrong PID to register the agent.

The solution proposed here checks whether os.getpid()'s parent is a python process, and if so assumes it is the launcher. It then sets this PID as the agent_pid. A different solution would be to pass the agent_pid in to the process as a command line parameter or an environment variable.

Related issue number

Closes #24361, #23945

Checks

Is there a test for running the ray head process in a virtualenv (different than the runtime_env tests, which run the workers in a virtualenv/conda env)?

Comment thread dashboard/agent.py
# not the python process PID as reported by os.getpid()
try:
pid = parent.pid
except AttributeError:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you comment when this exception is raised?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@rkooo567

Copy link
Copy Markdown
Contributor

Hmm, I actually think the other solution you suggested (A different solution would be to pass the agent_pid in to the process as a command line parameter or an environment variable.) seems a lot cleaner than the current approach. Is there any reason why we shouldn't go with the alternative approach?

@mattip

mattip commented May 19, 2022

Copy link
Copy Markdown
Contributor Author

Is there any reason why we shouldn't go with the alternative approach?

There are many ID types in src/ray/common/id.h. I am not sure how to best use one of them to create an AgentID in AgentManager::StartAgent that would be used instead of the PID. Could you suggest some code to do this?

Once we have a unique identifier (hopefully not too long), it should be trivial to add a command-line argument to use it and then parse that argument in agent.py

@rkooo567

Copy link
Copy Markdown
Contributor

The Process class, GetId should return the pid of the child processes!

@mattip

mattip commented May 19, 2022

Copy link
Copy Markdown
Contributor Author

GetId should return the pid of the child processes!

By then it is too late, how can I communicate that pid to the child process? The sequence is

  • In agent_manager.cc, build the command line arguments, including an --agent-id=<random number>
  • call Process child(args) which then starts the python process which should parse the agent-id off the command line
  • set up the agent manager callbacks to expect the agent-id rather than the PID
  • in python, register the process by calling the callbacks with the agent-id

I think I have a proof-of-concept using int agent_id = rand(), I am trying to get it to work. C++ is much slower than Python.

@mattip mattip mentioned this pull request May 19, 2022
1 of 6 tasks
@mattip

mattip commented May 19, 2022

Copy link
Copy Markdown
Contributor Author

closing in favor of #24968

@rkooo567 rkooo567 closed this May 20, 2022
rkooo567 pushed a commit that referenced this pull request May 27, 2022
When using ray inside a virtualenv on windows, python.exe as reported by sys.executable is a PEP397 launcher to the actual python as reported by os.getpid():

>>> import sys, os, psutil
>>> >>> print(sys.executable)
C:\temp\issue24361\Scripts\python.exe
>>> os.getpid()
2208
>>> child = psutil.Process(2208)
>>> child.cmdline()
['C:\\oss\\CPython38\\python.exe']
>>> child.parent().cmdline()
['C:\\temp\\issue24361\\Scripts\\python.exe']
>>> child.parent().pid
6424
When the agent_manager launches the agent process via Process::Process(), it gets the PID of the launcher process (6424), which is what is expected as an ID when registering the agent in the gRPC callback. But inside agent.py, the child process reports the PID via os.getpid(), which is 2208, and this is the wrong PID to register the agent.

The solution proposed here is another version of #24905 that creates a int agent_id = rand(); before starting the python process, and passes the agent_id to the process.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Core] ray.exceptions.LocalRayletDiedError: The task's local raylet died. Check raylet.out for more information.

2 participants