-
Notifications
You must be signed in to change notification settings - Fork 401
Fix process spawn #5634
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
Draft
marcotc
wants to merge
7
commits into
master
Choose a base branch
from
marco/issue-5621-process-spawn
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Fix process spawn #5634
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
622028b
Refactor spawn monkey patch regression specs
marcotc 620f6c9
Rename spawn monkey patch env provider
marcotc 30561aa
Fix spawn monkey patch env handling
marcotc dd29ca6
Document execute_in_fork alongside expect_in_fork
marcotc 5bb8953
Tighten spawn monkey patch array-form spec
marcotc 73d64aa
Update spawn monkey patch RBS
marcotc 567402b
Fix spawn monkey patch spec lint
marcotc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,15 +3,15 @@ module Datadog | |||||
| module Utils | ||||||
| module SpawnMonkeyPatch | ||||||
| # Set in apply! before Process.spawn is intercepted; internal wiring only. | ||||||
| self.@lineage_envs_provider: ^() -> ::Hash[::String, ::String] | ||||||
| self.@env_provider: ^() -> ::Hash[::String, ::String] | ||||||
|
|
||||||
| def self.apply!: (lineage_envs_provider: ^() -> ::Hash[::String, ::String]) -> true | ||||||
| def self.apply!: (env_provider: ^() -> ::Hash[::String, ::String]) -> untyped | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| module ProcessSpawnPatch | ||||||
| def spawn: (*untyped args, **untyped opts) -> untyped | ||||||
| def spawn: (*untyped args) -> untyped | ||||||
| end | ||||||
|
|
||||||
| def self.inject_lineage_envs: (untyped args) -> ::Array[untyped] | ||||||
| def self.inject_envs: (untyped args) -> ::Array[untyped] | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,8 +5,28 @@ | |||||||||||
| require 'datadog/core/configuration/settings' | ||||||||||||
|
|
||||||||||||
| RSpec.describe Datadog::Core::Utils::SpawnMonkeyPatch do | ||||||||||||
| let(:envs) do | ||||||||||||
| { | ||||||||||||
| 'ENV1' => 'val1', | ||||||||||||
| 'ENV2' => 'val2', | ||||||||||||
| } | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| def process_spawn(*spawn_args) | ||||||||||||
| IO.pipe do |read_io, write_io| | ||||||||||||
| process_options = {in: File::NULL, out: write_io, err: write_io} | ||||||||||||
| process_options.merge!(spawn_args.pop) if ::Hash === spawn_args.last | ||||||||||||
|
|
||||||||||||
| pid = Process.spawn(*spawn_args, process_options) | ||||||||||||
| write_io.close | ||||||||||||
| Process.wait(pid) | ||||||||||||
|
|
||||||||||||
| read_io.read.lines.map { |line| line.chomp.split('=', 2) }.to_h | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should fix the failing tests
Suggested change
|
||||||||||||
| end | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| describe '::apply!' do | ||||||||||||
| subject(:apply!) { described_class.apply!(lineage_envs_provider: -> { {} }) } | ||||||||||||
| subject(:apply!) { described_class.apply!(env_provider: -> { envs }) } | ||||||||||||
|
|
||||||||||||
| context 'when Process.spawn is supported' do | ||||||||||||
| before do | ||||||||||||
|
|
@@ -15,12 +35,86 @@ | |||||||||||
| end | ||||||||||||
|
|
||||||||||||
| it 'prepends the spawn monkey patch' do | ||||||||||||
| expect_in_fork do | ||||||||||||
| apply! | ||||||||||||
| expect(Process.singleton_class.ancestors).to include(described_class::ProcessSpawnPatch) | ||||||||||||
| expect(Process.method(:spawn).source_location.first).to match(/spawn_monkey_patch\.rb/) | ||||||||||||
| end | ||||||||||||
| apply! | ||||||||||||
| expect(Process.singleton_class.ancestors).to include(described_class::ProcessSpawnPatch) | ||||||||||||
| expect(Process.method(:spawn).source_location.first).to match(/spawn_monkey_patch\.rb/) | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| it 'does not patch twice' do | ||||||||||||
| described_class.apply!(env_provider: -> { {'ENV1' => 'val1'} }) | ||||||||||||
| described_class.apply!(env_provider: -> { {'ENV1' => 'val2'} }) | ||||||||||||
|
|
||||||||||||
| expect(Process.singleton_class.ancestors.count(described_class::ProcessSpawnPatch)).to eq(1) | ||||||||||||
| expect(process_spawn('/usr/bin/env')).to include('ENV1' => 'val2') | ||||||||||||
| end | ||||||||||||
| end | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| describe 'on Process.spawn' do | ||||||||||||
| subject(:apply!) { described_class.apply!(env_provider: -> { envs }) } | ||||||||||||
|
|
||||||||||||
| around do |example| | ||||||||||||
| ClimateControl.modify('PARENT1' => 'parent_val') { example.run } | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| before do | ||||||||||||
| skip 'Process.spawn not supported' unless Process.respond_to?(:spawn) | ||||||||||||
| apply! | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| it 'merges env_provider, parent envs, and env argument' do | ||||||||||||
| output = process_spawn({'ARG' => 'arg_val'}, '/usr/bin/env', pgroup: true) | ||||||||||||
|
|
||||||||||||
| expect(output).to include('PARENT1' => 'parent_val', 'ENV1' => 'val1', 'ENV2' => 'val2', 'ARG' => 'arg_val') | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| it 'merges env_provider and parent envs when no env argument is provided' do | ||||||||||||
| output = process_spawn('/usr/bin/env', pgroup: true) | ||||||||||||
|
|
||||||||||||
| expect(output).to include('PARENT1' => 'parent_val', 'ENV1' => 'val1', 'ENV2' => 'val2') | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| it 'respects parent env removal through the value `nil`' do | ||||||||||||
| output = process_spawn({'PARENT1' => nil}, '/usr/bin/env') | ||||||||||||
|
|
||||||||||||
| expect(output).not_to include('PARENT1') | ||||||||||||
| expect(output).to include('ENV1' => 'val1', 'ENV2' => 'val2') | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| it 'respects array-form command variant' do | ||||||||||||
| command = 'printf %s "$0:$ARG:$PARENT1:$ENV1:$ENV2"' | ||||||||||||
|
|
||||||||||||
| output = IO.pipe do |read_io, write_io| | ||||||||||||
| pid = Process.spawn( | ||||||||||||
| {'ARG' => 'arg_val'}, | ||||||||||||
| ['/bin/sh', 'cmd-name'], | ||||||||||||
| '-c', | ||||||||||||
| command, | ||||||||||||
| in: File::NULL, | ||||||||||||
| out: write_io, | ||||||||||||
| err: write_io, | ||||||||||||
| ) | ||||||||||||
| write_io.close | ||||||||||||
| Process.wait(pid) | ||||||||||||
|
|
||||||||||||
| read_io.read | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| expect(output).to eq('cmd-name:arg_val:parent_val:val1:val2') | ||||||||||||
| end | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| describe '::inject_envs' do | ||||||||||||
| subject(:inject_envs) { described_class.inject_envs(args.dup) } | ||||||||||||
| let(:args) { [env_argument, '/bin/ls', '.', {pgroup: 0}] } | ||||||||||||
| let(:env_argument) { {'TZ' => 'UTC'} } | ||||||||||||
|
|
||||||||||||
| before do | ||||||||||||
| described_class.apply!(env_provider: -> { envs }) | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| it 'does not mutate the provided env argument Hash' do | ||||||||||||
| expect { inject_envs }.not_to change { env_argument } | ||||||||||||
| end | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
|
|
@@ -33,16 +127,14 @@ | |||||||||||
| end | ||||||||||||
|
|
||||||||||||
| it 'applies both fork and spawn patches when Components is initialized' do | ||||||||||||
| expect_in_fork do | ||||||||||||
| Datadog::Core::Configuration::Components.new(Datadog::Core::Configuration::Settings.new) | ||||||||||||
| Datadog::Core::Configuration::Components.new(Datadog::Core::Configuration::Settings.new) | ||||||||||||
|
|
||||||||||||
| expect(Process.singleton_class.ancestors).to include( | ||||||||||||
| Datadog::Core::Utils::AtForkMonkeyPatch::ProcessMonkeyPatch, | ||||||||||||
| ) | ||||||||||||
| expect(Process.singleton_class.ancestors).to include( | ||||||||||||
| Datadog::Core::Utils::SpawnMonkeyPatch::ProcessSpawnPatch, | ||||||||||||
| ) | ||||||||||||
| end | ||||||||||||
| expect(Process.singleton_class.ancestors).to include( | ||||||||||||
| Datadog::Core::Utils::AtForkMonkeyPatch::ProcessMonkeyPatch, | ||||||||||||
| ) | ||||||||||||
| expect(Process.singleton_class.ancestors).to include( | ||||||||||||
| Datadog::Core::Utils::SpawnMonkeyPatch::ProcessSpawnPatch, | ||||||||||||
| ) | ||||||||||||
| end | ||||||||||||
| end | ||||||||||||
| end | ||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: We can improve clarity