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

fix!: handle socket name collisions #125

Merged
merged 12 commits into from
Jun 3, 2024

Conversation

jericht
Copy link
Contributor

@jericht jericht commented Apr 30, 2024

What was the problem/requirement? (What/Why)

  • The unix socket names created by the adaptor runtime can collide once PIDs are reused by the OS. Normally, these files are cleaned up, but if the runtime crashes, they can be left around and cause collision issues.

What was the solution? (How)

  • Have the socket creation code check if the socket already exists. If it does, keep checking if <pid>_<counter> exists until it is available and use that.
  • Sockets are now created in the system temp directory.
  • Added daemon bootstrap logging (via a new --log-file option only exposed in the _serve command) so that if the daemon bootstrap fails, we are able to look at the logs and determine what went wrong. Daemon bootstrap logging stops logging to the file after bootstrap is complete.
  • Improved the --connection-file CLI help text
  • Removed the worker path component from configuration file paths
  • Add support for configuring connection data via environment variables. Currently, there is only one env var needed, OPENJD_ADAPTOR_SOCKET.
    • The <adaptor> daemon start command has been updated to emit an openjd_env: OPENJD_ADAPTOR_SOCKET=<path-to-socket> line to stdout so that, when running in an OpenJD environment, the option is automatically set and subsequent commands within this OpenJD environment no longer need to pass in a --connection-file argument.
  • The --connection-file argument is no longer necessary if OPENJD_ADAPTOR_SOCKET is provided
    • The <adaptor> daemon start command will still generate a connection file at a temporary directory when it is not provided, then delete it after daemon bootstrap is complete.
  • Small cleanup/fixes:
    • Rename classes/methods sockets.py module to refer to "paths" rather than "directories"
    • Fix unit/integ tests expecting unix style paths
    • Moved OpenJD constants into a _utils._constants.py file

What is the impact of this change?

  • Adaptor runtime handles socket name collisions now
  • OpenJD job templates that run <adaptor> daemon start in an OpenJD environment no longer need to pass --connection-file anymore

How was this change tested?

  • Added/updated unit tests
  • Added integration test
  • Manual testing, see below

Was this change documented?

No

Is this a breaking change?

No, the modified interface is internal to this package, under the _http package

Manual testing

Test adaptor files

main.py

import openjd.adaptor_runtime as runtime
from .adaptor import FakeAdaptor

if __name__ == "__main__":
    try:
        runtime.EntryPoint(FakeAdaptor).start()
    except Exception as e:
        print(f"Unexpected error in adaptor main: {e}")
        raise
    else:
        print("nice")

adaptor.py

import openjd.adaptor_runtime.adaptors as adaptors
class FakeAdaptor(adaptors.BaseAdaptor):
    def __init__(self, init_data: dict, **kwargs):
        super().__init__(init_data, **kwargs)
    @property
    def integration_data_interface_version(self) -> adaptors.SemanticVersion:
        return adaptors.SemanticVersion(major=0, minor=1)
    def _start(self):
        pass
    def _run(self, run_data: dict):
        pass
    def _cleanup(self):
        pass
    def _stop(self):
        pass

Test logs - Linux with Env Var

(fake-adaptor) $ fake-adaptor daemon start
INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/runtime/configuration.json
INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
INFO: Initializing backend process...
INFO: Started backend process. PID: 5089
INFO: Waiting for File '/tmp/ojd-ar-_n9r_2og/connection.json' to exist
INFO: Retrying in 1s...
INFO: Waiting for File '/tmp/ojd-ar-_n9r_2og/connection.json' to be openable
INFO: Waiting for File '/tmp/ojd-ar-_n9r_2og/connection.json' to have valid ConnectionSettings
INFO: ========== BEGIN BOOTSTRAP OUTPUT CONTENTS ==========
INFO: INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/runtime/configuration.json
INFO: INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
INFO: ========== END BOOTSTRAP OUTPUT CONTENTS ==========
INFO: Checking for bootstrap logs at '/tmp/adaptor-runtime-background-bootstrap-191fb704-90c3-4e06-a399-8fc7c12c231e.log'
INFO: ========== BEGIN BOOTSTRAP LOG CONTENTS ==========
INFO: [2024-05-28 21:49:36,443][INFO    ] Applying user-level configuration: /home/jericht/.openjd/adaptors/runtime/configuration.json
INFO: [2024-05-28 21:49:36,445][INFO    ] Applying user-level configuration: /home/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
INFO: [2024-05-28 21:49:36,445][INFO    ] Running in background daemon mode.
INFO: ========== END BOOTSTRAP LOG CONTENTS ==========
INFO: Verifying connection to backend...
INFO: Connected successfully
openjd_env: OPENJD_ADAPTOR_SOCKET=/tmp/.openjd_adaptor_runtime/5089
ADAPTOR_OUTPUT: INFO: Running in background daemon mode.
nice
(fake-adaptor) $ export OPENJD_ADAPTOR_SOCKET=/tmp/.openjd_adaptor_runtime/5089
(fake-adaptor) $ fake-adaptor daemon run
INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/runtime/configuration.json
INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
ADAPTOR_OUTPUT: 
nice
(fake-adaptor) $ fake-adaptor daemon stop
INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/runtime/configuration.json
INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
ADAPTOR_OUTPUT: 
ADAPTOR_OUTPUT: INFO: Daemon background process stopped.
nice

Test Logs - Linux with connection file

(fake-adaptor) $ fake-adaptor daemon start --connection-file /tmp/connection.json
INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/runtime/configuration.json
INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
INFO: Initializing backend process...
INFO: Started backend process. PID: 5556
INFO: Waiting for File '/tmp/connection.json' to exist
INFO: Retrying in 1s...
INFO: Waiting for File '/tmp/connection.json' to be openable
INFO: Waiting for File '/tmp/connection.json' to have valid ConnectionSettings
INFO: ========== BEGIN BOOTSTRAP OUTPUT CONTENTS ==========
INFO: INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/runtime/configuration.json
INFO: INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
INFO: ========== END BOOTSTRAP OUTPUT CONTENTS ==========
INFO: Checking for bootstrap logs at '/tmp/adaptor-runtime-background-bootstrap-edca8ed7-1ca7-41ef-844b-904aa6b9afa6.log'
INFO: ========== BEGIN BOOTSTRAP LOG CONTENTS ==========
INFO: [2024-05-28 21:50:09,943][INFO    ] Applying user-level configuration: /home/jericht/.openjd/adaptors/runtime/configuration.json
INFO: [2024-05-28 21:50:09,945][INFO    ] Applying user-level configuration: /home/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
INFO: [2024-05-28 21:50:09,945][INFO    ] Running in background daemon mode.
INFO: ========== END BOOTSTRAP LOG CONTENTS ==========
INFO: Verifying connection to backend...
INFO: Connected successfully
openjd_env: OPENJD_ADAPTOR_SOCKET=/tmp/.openjd_adaptor_runtime/5556
ADAPTOR_OUTPUT: INFO: Running in background daemon mode.
nice
(fake-adaptor) $ cat /tmp/connection.json 
{"socket": "/tmp/.openjd_adaptor_runtime/5556"}(fake-adaptor) $ 
(fake-adaptor) $ fake-adaptor daemon run --connection-file /tmp/connection.json
INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/runtime/configuration.json
INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
ADAPTOR_OUTPUT: 
nice
(fake-adaptor) $ fake-adaptor daemon stop --connection-file /tmp/connection.json
INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/runtime/configuration.json
INFO: Applying user-level configuration: /home/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
ADAPTOR_OUTPUT: 
ADAPTOR_OUTPUT: INFO: Daemon background process stopped.
nice

Test Logs - Mac with env var

(fake-adaptor) jericht fake-adaptor % fake-adaptor daemon start
INFO: Creating empty configuration at /Users/jericht/.openjd/adaptors/runtime/configuration.json
INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/runtime/configuration.json
INFO: Creating empty configuration at /Users/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
INFO: Initializing backend process...
INFO: Started backend process. PID: 41645
INFO: Waiting for File '/var/folders/1z/3kw8_st57n9c_wsjd5n898wh0000gq/T/ojd-ar-__td_97m/connection.json' to exist
INFO: Retrying in 1s...
INFO: Waiting for File '/var/folders/1z/3kw8_st57n9c_wsjd5n898wh0000gq/T/ojd-ar-__td_97m/connection.json' to be openable
INFO: Waiting for File '/var/folders/1z/3kw8_st57n9c_wsjd5n898wh0000gq/T/ojd-ar-__td_97m/connection.json' to have valid ConnectionSettings
INFO: ========== BEGIN BOOTSTRAP OUTPUT CONTENTS ==========
INFO: INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/runtime/configuration.json
INFO: INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
INFO: ========== END BOOTSTRAP OUTPUT CONTENTS ==========
INFO: Checking for bootstrap logs at '/var/folders/1z/3kw8_st57n9c_wsjd5n898wh0000gq/T/adaptor-runtime-background-bootstrap-7333d334-3f39-4b27-8f59-a1465611f1bc.log'
INFO: ========== BEGIN BOOTSTRAP LOG CONTENTS ==========
INFO: [2024-05-28 14:44:45,339][INFO    ] Applying user-level configuration: /Users/jericht/.openjd/adaptors/runtime/configuration.json
INFO: [2024-05-28 14:44:45,340][INFO    ] Applying user-level configuration: /Users/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
INFO: [2024-05-28 14:44:45,341][INFO    ] Running in background daemon mode.
INFO: ========== END BOOTSTRAP LOG CONTENTS ==========
INFO: Verifying connection to backend...
INFO: Connected successfully
openjd_env: OPENJD_ADAPTOR_SOCKET=/private/var/folders/1z/3kw8_st57n9c_wsjd5n898wh0000gq/T/.openjd_adaptor_runtime/41645
ADAPTOR_OUTPUT: INFO: Running in background daemon mode.
nice
(fake-adaptor) jericht fake-adaptor % export OPENJD_ADAPTOR_SOCKET=/private/var/folders/1z/3kw8_st57n9c_wsjd5n898wh0000gq/T/.openjd_adaptor_runtime/41645 
(fake-adaptor) jericht fake-adaptor % fake-adaptor daemon run
INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/runtime/configuration.json
INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
ADAPTOR_OUTPUT: 
nice
(fake-adaptor) jericht fake-adaptor % fake-adaptor daemon stop
INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/runtime/configuration.json
INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
ADAPTOR_OUTPUT: 
ADAPTOR_OUTPUT: INFO: Daemon background process stopped.
nice

Test Logs - Mac with connection file

(fake-adaptor) jericht fake-adaptor % fake-adaptor daemon start --connection-file /tmp/connection.json
INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/runtime/configuration.json
INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
INFO: Initializing backend process...
INFO: Started backend process. PID: 41858
INFO: Waiting for File '/tmp/connection.json' to exist
INFO: Retrying in 1s...
INFO: Waiting for File '/tmp/connection.json' to be openable
INFO: Waiting for File '/tmp/connection.json' to have valid ConnectionSettings
INFO: ========== BEGIN BOOTSTRAP OUTPUT CONTENTS ==========
INFO: INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/runtime/configuration.json
INFO: INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
INFO: ========== END BOOTSTRAP OUTPUT CONTENTS ==========
INFO: Checking for bootstrap logs at '/var/folders/1z/3kw8_st57n9c_wsjd5n898wh0000gq/T/adaptor-runtime-background-bootstrap-954cf37a-0839-423c-aa70-90e46d6aefb6.log'
INFO: ========== BEGIN BOOTSTRAP LOG CONTENTS ==========
INFO: [2024-05-28 14:46:36,493][INFO    ] Applying user-level configuration: /Users/jericht/.openjd/adaptors/runtime/configuration.json
INFO: [2024-05-28 14:46:36,494][INFO    ] Applying user-level configuration: /Users/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
INFO: [2024-05-28 14:46:36,494][INFO    ] Running in background daemon mode.
INFO: ========== END BOOTSTRAP LOG CONTENTS ==========
INFO: Verifying connection to backend...
INFO: Connected successfully
openjd_env: OPENJD_ADAPTOR_SOCKET=/private/var/folders/1z/3kw8_st57n9c_wsjd5n898wh0000gq/T/.openjd_adaptor_runtime/41858
ADAPTOR_OUTPUT: INFO: Running in background daemon mode.
nice
(fake-adaptor) jericht fake-adaptor % cat /tmp/connection.json 
{"socket": "/private/var/folders/1z/3kw8_st57n9c_wsjd5n898wh0000gq/T/.openjd_adaptor_runtime/41858"}%                                          (fake-adaptor) jericht fake-adaptor % fake-adaptor daemon run --connection-file /tmp/connection.json
INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/runtime/configuration.json
INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
ADAPTOR_OUTPUT: 
nice
(fake-adaptor) jericht fake-adaptor % fake-adaptor daemon stop --connection-file /tmp/connection.json
INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/runtime/configuration.json
INFO: Applying user-level configuration: /Users/jericht/.openjd/adaptors/FakeAdaptor/FakeAdaptor.json
ADAPTOR_OUTPUT: 
ADAPTOR_OUTPUT: INFO: Daemon background process stopped.
nice

Test Logs - Windows with env var

(fake-adaptor) C:\Users\Administrator\Desktop\fake-adaptor>fake-adaptor daemon start
INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\runtime\configuration.json
INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\FakeAdaptor\FakeAdaptor.json
INFO: Initializing backend process...
INFO: Started backend process. PID: 2336
INFO: Waiting for File 'C:\Users\ADMINI~1\AppData\Local\Temp\2\ojd-ar-72f64zef\connection.json' to exist
INFO: Retrying in 1s...
INFO: Waiting for File 'C:\Users\ADMINI~1\AppData\Local\Temp\2\ojd-ar-72f64zef\connection.json' to be openable
INFO: Waiting for File 'C:\Users\ADMINI~1\AppData\Local\Temp\2\ojd-ar-72f64zef\connection.json' to have valid ConnectionSettings
INFO: ========== BEGIN BOOTSTRAP OUTPUT CONTENTS ==========
INFO: INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\runtime\configuration.json
INFO: INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\FakeAdaptor\FakeAdaptor.json
INFO: ========== END BOOTSTRAP OUTPUT CONTENTS ==========
INFO: Checking for bootstrap logs at 'C:\Users\ADMINI~1\AppData\Local\Temp\2\adaptor-runtime-background-bootstrap-2fd74b9e-188a-456e-a4ed-10dc470b9623.log'
INFO: ========== BEGIN BOOTSTRAP LOG CONTENTS ==========
INFO: [2024-05-28 21:57:12,622][INFO    ] Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\runtime\configuration.json
INFO: [2024-05-28 21:57:12,622][INFO    ] Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\FakeAdaptor\FakeAdaptor.json
INFO: [2024-05-28 21:57:12,622][INFO    ] Running in background daemon mode.
INFO: [2024-05-28 21:57:12,622][INFO    ] Creating Named Pipe with name: \\.\pipe\AdaptorNamedPipe_5612
INFO: ========== END BOOTSTRAP LOG CONTENTS ==========
INFO: Verifying connection to backend...
INFO: Connected successfully
openjd_env: OPENJD_ADAPTOR_SOCKET=\\.\pipe\AdaptorNamedPipe_5612
ADAPTOR_OUTPUT: INFO: Running in background daemon mode.
ADAPTOR_OUTPUT: INFO: Creating Named Pipe with name: \\.\pipe\AdaptorNamedPipe_5612
nice

(fake-adaptor) C:\Users\Administrator\Desktop\fake-adaptor>set "OPENJD_ADAPTOR_SOCKET=\\.\pipe\AdaptorNamedPipe_5612"

(fake-adaptor) C:\Users\Administrator\Desktop\fake-adaptor>fake-adaptor daemon run
INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\runtime\configuration.json
INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\FakeAdaptor\FakeAdaptor.json
ADAPTOR_OUTPUT:
nice

(fake-adaptor) C:\Users\Administrator\Desktop\fake-adaptor>fake-adaptor daemon stop
INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\runtime\configuration.json
INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\FakeAdaptor\FakeAdaptor.json
ADAPTOR_OUTPUT:
ADAPTOR_OUTPUT: INFO: Daemon background process stopped.
nice

Test Logs - Windows with connection file

(fake-adaptor) C:\Users\Administrator\Desktop\fake-adaptor>fake-adaptor daemon start --connection-file connection.json
INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\runtime\configuration.json
INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\FakeAdaptor\FakeAdaptor.json
INFO: Initializing backend process...
INFO: Started backend process. PID: 2092
INFO: Waiting for File 'C:\Users\Administrator\Desktop\fake-adaptor\connection.json' to exist
INFO: Retrying in 1s...
INFO: Waiting for File 'C:\Users\Administrator\Desktop\fake-adaptor\connection.json' to be openable
INFO: Waiting for File 'C:\Users\Administrator\Desktop\fake-adaptor\connection.json' to have valid ConnectionSettings
INFO: ========== BEGIN BOOTSTRAP OUTPUT CONTENTS ==========
INFO: INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\runtime\configuration.json
INFO: INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\FakeAdaptor\FakeAdaptor.json
INFO: ========== END BOOTSTRAP OUTPUT CONTENTS ==========
INFO: Checking for bootstrap logs at 'C:\Users\ADMINI~1\AppData\Local\Temp\2\adaptor-runtime-background-bootstrap-cc17769b-250e-43ce-8296-c43bc7b8486b.log'
INFO: ========== BEGIN BOOTSTRAP LOG CONTENTS ==========
INFO: [2024-05-28 21:56:15,434][INFO    ] Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\runtime\configuration.json
INFO: [2024-05-28 21:56:15,434][INFO    ] Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\FakeAdaptor\FakeAdaptor.json
INFO: [2024-05-28 21:56:15,434][INFO    ] Running in background daemon mode.
INFO: [2024-05-28 21:56:15,434][INFO    ] Creating Named Pipe with name: \\.\pipe\AdaptorNamedPipe_2084
INFO: ========== END BOOTSTRAP LOG CONTENTS ==========
INFO: Verifying connection to backend...
INFO: Connected successfully
openjd_env: OPENJD_ADAPTOR_SOCKET=\\.\pipe\AdaptorNamedPipe_2084
ADAPTOR_OUTPUT: INFO: Running in background daemon mode.
ADAPTOR_OUTPUT: INFO: Creating Named Pipe with name: \\.\pipe\AdaptorNamedPipe_2084
nice

(fake-adaptor) C:\Users\Administrator\Desktop\fake-adaptor>type connection.json
{"socket": "\\\\.\\pipe\\AdaptorNamedPipe_2084"}
(fake-adaptor) C:\Users\Administrator\Desktop\fake-adaptor>fake-adaptor daemon run --connection-file connection.json
INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\runtime\configuration.json
INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\FakeAdaptor\FakeAdaptor.json
ADAPTOR_OUTPUT:
nice

(fake-adaptor) C:\Users\Administrator\Desktop\fake-adaptor>fake-adaptor daemon stop --connection-file connection.json
INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\runtime\configuration.json
INFO: Applying user-level configuration: C:\Users\Administrator\.openjd\adaptors\FakeAdaptor\FakeAdaptor.json
ADAPTOR_OUTPUT:
ADAPTOR_OUTPUT: INFO: Daemon background process stopped.
nice

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@jericht jericht requested a review from a team as a code owner April 30, 2024 08:44
@jericht jericht marked this pull request as draft May 1, 2024 02:34
@jericht jericht force-pushed the jericht/socket_collision branch 4 times, most recently from 7e57788 to 0f651bd Compare May 2, 2024 01:30
@jericht
Copy link
Contributor Author

jericht commented May 2, 2024

I've added unit tests that work, as well as an integration tests that is not working. I will be debugging that tomorrow, probably by adding a bootstrap log to the backend process so I can actually debug what's going on.

@jericht jericht force-pushed the jericht/socket_collision branch 3 times, most recently from e2373d0 to 5c72013 Compare May 7, 2024 21:26
@jericht jericht marked this pull request as ready for review May 7, 2024 21:34
@jericht jericht changed the title fix: handle unix socket name collisions feat: add --working-dir parameter to supersede --connection-file May 7, 2024
@jericht
Copy link
Contributor Author

jericht commented May 7, 2024

Discussed with the team, we will be switching this to using environment variables instead, another revision coming

@jericht jericht marked this pull request as draft May 7, 2024 23:38
@jericht jericht changed the title feat: add --working-dir parameter to supersede --connection-file feat: add OPENJD_ADAPTOR_SOCKET environment variable May 15, 2024
@jericht jericht changed the title feat: add OPENJD_ADAPTOR_SOCKET environment variable fix: handle socket name collisions May 15, 2024
@jericht jericht marked this pull request as ready for review May 15, 2024 00:56
@jericht jericht changed the title fix: handle socket name collisions fix!: handle socket name collisions May 15, 2024
@jericht jericht changed the title fix!: handle socket name collisions fix: handle socket name collisions May 15, 2024
@jericht jericht marked this pull request as draft May 15, 2024 01:13
Comment on lines +69 to +71
connection_settings (ConnectionSettings, optional): The connection settings to use.
This option is not required for the "init" command, but is required for everything
else. Defaults to None.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a detail here mentioning that the connection_settings are initialized in the init() command?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure

Comment on lines +75 to +78
"The file path to the connection file for use in daemon mode. For the 'daemon start' command, this file "
"must not exist. For all other commands, this file must exist. This option is highly "
"recommended if using the adaptor in an interactive terminal. Default is to read the "
f"connection data from the environment variable: {_OPENJD_ADAPTOR_SOCKET_ENV}"
Copy link
Contributor

Choose a reason for hiding this comment

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

I like that you added some clarification on what the connection file is, but I think we could improve this a bit. How do you feel about something along the lines of

Used when running in daemon mode. Connection data is written to this filepath during the 'daemon start' 
command and read in the 'daemon run/stop/cleanup' commands. If --connection-file is not provided then the
'{_OPENJD_ADAPTOR_SOCKET_ENV}' environment variable must be set with the connection file path.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll add this

"recommended if using the adaptor in an interactive terminal. Default is to read the "
f"connection data from the environment variable: {_OPENJD_ADAPTOR_SOCKET_ENV}"
),
"log_file": "The file to log adaptor output to. Default is to not log to a file.",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"log_file": "The file to log adaptor output to. Default is to not log to a file.",
"bootstrap_log_file": "Only used in daemon _serve. An optional file path to log connection bootstrap output to.",

Copy link
Contributor

Choose a reason for hiding this comment

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

If we leave it as log_file it seems like it should contain all adaptor logs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we can punt that to a future change. This change has already grown far too large in scope. Does that sound ok?

Copy link
Contributor

Choose a reason for hiding this comment

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

If we make this change in the future it then becomes another breaking change whereas making it before this is merged isn't breaking. In this case we're renaming an argument to make it (in my opinion) more clear what the purpose is so I'd prefer if we adopted it in this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the parameter name to --bootstrap-log-file

@@ -64,7 +88,6 @@
os.path.join(
_system_config_path_prefix,
"openjd",
"worker",
Copy link
Contributor

Choose a reason for hiding this comment

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

This is also a breaking change right? Just want to make sure we call it out in the commit so that it is available in the changelog

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, I'll update the commit

@@ -123,13 +159,21 @@ class EntryPoint:
The main entry point of the adaptor runtime.
"""

on_bootstrap_complete: MutableSet[Callable[[], None]]
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure that I like the list of callbacks pattern established here and above in the Background Runner. Right now we have a

  • Set of callbacks in the _LogConfig which we add a single callback to in order to disconnect the file log handlers, but removes itself from the set of callbacks when called.
  • A set of callbacks in the BackgroundRunner which is only invoked when the connection file is written, which we added in order to provide the aforementioned callback

It looks like the goal is to disconnect the bootstrap logger after the connection file is written. I appreciate the forward thinking but I think in this case I'd prefer biasing towards keeping things simple and just having the background runner call a function directly which disconnects the bootstrap logging. It would allow us to keep the flow between these components more directly readable

Copy link
Contributor Author

@jericht jericht May 16, 2024

Choose a reason for hiding this comment

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

Yeah, the idea is to make it generic so we do not need to refactor it in the future if we ever need to subscribe more actions to when the bootstrap is complete. Here I am trying to follow a really basic version of "events" in other languages, like C#.

Set of callbacks in the _LogConfig which we add a single callback to in order to disconnect the file log handlers, but removes itself from the set of callbacks when called.

This is an implementation detail of the event handler. It is designed to unsubscribe from the event after it's handled the first occurrence.

A set of callbacks in the BackgroundRunner which is only invoked when the connection file is written, which we added in order to provide the aforementioned callback

The fact that it happens after the connection file is written should be abstracted away from the caller. I named it on_bootstrap_complete so that callers know that it happens after the BackendRunner is done bootstrapping, whatever that means (currently it means after the connection file is written, but may change in the future if we need to add more bootstrapping actions).

Comment on lines +378 to +382
if not connection_file:
raise RuntimeError(
"--connection file is required for the '_serve' command but was not provided."
)

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason we don't read from the environment variable here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah good point, I will add that ability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, thinking about this further. The environment variable OPENJD_ADAPTOR_SOCKET points to the path for the socket that should be used for IPC, so that's different from the connection file. Currently, the BackendRunner is responsible for creating the socket and communicating that back to the frontend (via the --connection-file). We can change the backend to accept a path to try to create the socket into, however that currently does not happen.

Since the _serve command is intended to be an "internal" command and is hidden from CLI help text, I think we can skip this for now. Does that sound ok?

@jericht jericht changed the title fix: handle socket name collisions fix!: handle socket name collisions May 16, 2024
@jericht jericht force-pushed the jericht/socket_collision branch 3 times, most recently from dbc8a5d to 6ff4dab Compare May 21, 2024 17:00
@jericht jericht marked this pull request as ready for review May 21, 2024 17:04
@jericht jericht marked this pull request as draft May 21, 2024 18:54
@jericht jericht force-pushed the jericht/socket_collision branch 2 times, most recently from d28608b to 4e7f93e Compare May 23, 2024 18:36
@jericht jericht force-pushed the jericht/socket_collision branch 5 times, most recently from 302f267 to 544873b Compare May 28, 2024 21:27
jericht added 11 commits May 28, 2024 21:31
Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com>
Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com>
Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com>
Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com>
Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com>
Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com>
Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com>
Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com>
Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com>
Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com>
Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com>
@jericht jericht marked this pull request as ready for review May 28, 2024 21:51
@ddneilson ddneilson self-requested a review May 31, 2024 14:57
ddneilson
ddneilson previously approved these changes May 31, 2024
Copy link
Contributor

@ddneilson ddneilson left a comment

Choose a reason for hiding this comment

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

This is great stuff, Jericho. Thanks!

else:
return True

wait_for(
Copy link
Contributor

Choose a reason for hiding this comment

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

Hah, nice. I actually just filed a bug for this. It looked like we've seen some data races on windows where the file can be opened, but hadn't yet contained data. Thanks for fixing!

Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com>
Copy link

sonarcloud bot commented May 31, 2024

Quality Gate Passed Quality Gate passed

Issues
5 New issues
1 Accepted issue

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@jericht jericht merged commit a123717 into OpenJobDescription:mainline Jun 3, 2024
16 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.

3 participants