Skip to content

Improve autoref proc management#3470

Merged
Lmh-java merged 3 commits into
masterfrom
lmh/fix-performance
Jun 27, 2025
Merged

Improve autoref proc management#3470
Lmh-java merged 3 commits into
masterfrom
lmh/fix-performance

Conversation

@Lmh-java

@Lmh-java Lmh-java commented Jun 23, 2025

Copy link
Copy Markdown
Contributor

Description

We've been investigating the performance degradation of thunderscope over time.Running Thunderscope repeatedly causes your laptop to become increasingly slow.

One of the reasons is that we are not properly cleaning up autoref process after the shutdown thunderscope.

For spinning up autoref, we are Popening a shell script (wrapper), which configures JAVA_HOME and run the auto ref bin. However, the shell script wrapper and the actual autoref (java) are two separated processes with separeted pids. Popen only manages and kills the shell script process, but not the actual autoref.

After I run thunderscope and properly shutdown 3 times, I can see 3 java procs running in the background.

image

This PR fixes the issue by directly popening the autoref binary.

Testing Done

  1. Manual testing
  2. CI

Resolved Issues

Length Justification and Key Files to Review

Review Checklist

It is the reviewers responsibility to also make sure every item here has been covered

  • Function & Class comments: All function definitions (usually in the .h file) should have a javadoc style comment at the start of them. For examples, see the functions defined in thunderbots/software/geom. Similarly, all classes should have an associated Javadoc comment explaining the purpose of the class.
  • Remove all commented out code
  • Remove extra print statements: for example, those just used for testing
  • Resolve all TODO's: All TODO (or similar) statements should either be completed or associated with a github issue

- improve exiting speed
- properly handle the dangling thread
@Lmh-java

Copy link
Copy Markdown
Contributor Author

Didn't remove the auto_ref.sh wrapper, in case someone uses it to run autoref manually. :)

@Lmh-java Lmh-java marked this pull request as ready for review June 23, 2025 08:36
Comment on lines +223 to +225
env = os.environ.copy()
env["JAVA_HOME"] = "/opt/tbotspython/bin/jdk"
autoref_cmd = "/opt/tbotspython/autoReferee/bin/autoReferee -a"

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.

will this affect the environment variables of the current shell process (aka the shell that's running the bazel run command?). I think that is why I originally made a separate shell script

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.

No, I don't think so. Since we did os.environ.copy(), the environment vars are copied, so we are only modifying the copied version. Interestingly, didn't find offical Python doc about this, but using this as a reference. python/cpython#120836

with open(os.devnull, "w") as fp:
self.tigers_autoref_proc = Popen(
autoref_cmd.split(" "), stdout=fp, stderr=fp
autoref_cmd.split(" "), stdout=fp, stderr=fp, env=env

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.

I believe you need to do the same on line 239

@itsarune itsarune left a comment

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.

nice catch! unfortunately this doesn't run correctly. Autoref expects the binary to be called in a specific directory with respect to the binary, so you'd need to set the working directory to /opt/tbotspython/autoReferee/ before launching it.

@Lmh-java

Lmh-java commented Jun 24, 2025

Copy link
Copy Markdown
Contributor Author

nice catch! unfortunately this doesn't run correctly. Autoref expects the binary to be called in a specific directory with respect to the binary, so you'd need to set the working directory to /opt/tbotspython/autoReferee/ before launching it.

Whoops, they hard-coded their config filepath. Should be good now, waiting for the CI.

@Andrewyx Andrewyx left a comment

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.

Nice catch

@williamckha williamckha left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

with open(os.devnull, "w") as fp:
self.tigers_autoref_proc = Popen(
autoref_cmd.split(" "), stdout=fp, stderr=fp
autoref_cmd.split(" "),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

split() also works here, since default separator is any whitespace

autoref_proto_unix_io.register_observer(Referee, self.referee_buffer)

def __exit__(self, type, value, traceback) -> None:
self.end_autoref = True

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unrelated to your PR, but we ought to use Event here (and in all the other process managers) instead of a bool flag. Opened #3471 to track this issue

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.

For sure, I will take that ticket and implement the above comment along as well (so I don't get dismissed)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

oh awesome, thanks!

@Lmh-java Lmh-java merged commit 473badd into master Jun 27, 2025
6 checks passed
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.

4 participants