Skip to content

Commit

Permalink
Prevent from propagating env vars at outside of fork task scope
Browse files Browse the repository at this point in the history
Since deserialized context value of Token model by getting through
AttributeSet#attributes is passed by reference, additional env values
passed to create_child_token method overwrite a context which a token
instance variable has. This cause unintentional env values propagation
like KUROKO2_PARALLEL_FORK_SIZE at outside of the fork task scope.

To avoid this, use the deep copy for context value instead.
  • Loading branch information
Takuya Kosugiyama committed Jun 2, 2019
1 parent 0a86a4d commit 78c67be
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/autoload/kuroko2/workflow/task/fork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ def extract_child_nodes
end

def create_child_token(child_node:, env: {})
attributes = token.attributes.except('id', 'uuid', 'script', 'path', 'message', 'created_at', 'updated_at')
attributes = token.attributes.except('id', 'uuid', 'script', 'path', 'message', 'created_at', 'updated_at', 'context')
attributes = attributes.merge(uuid: SecureRandom.uuid, parent: token, script: child_node.to_script, path: '/')
attributes['context'] = token.context.deep_dup
attributes['context']['ENV'] = (attributes['context']['ENV'] || {}).merge(env)

Token.create!(attributes).tap do |created|
Expand Down
8 changes: 8 additions & 0 deletions spec/workflow/engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ module Kuroko2::Workflow
parallel_fork: 3
noop: noop1
noop: noop2
noop: noop3
EOF
end

Expand Down Expand Up @@ -173,6 +174,13 @@ module Kuroko2::Workflow
expect(parallel_tokens.map(&:path)).to all(eq('/0-sequence/1-noop'))
expect(parallel_tokens.map(&:status_name)).to all(eq('finished'))

subject.process(token)
expect(token.path).to eq '/2-noop'
expect(token.status_name).to eq 'working'
expect(token.context['ENV']['GLOBAL_ENV']).to eq('g')
expect(token.context['ENV']['KUROKO2_PARALLEL_FORK_SIZE']).to be_nil
expect(token.context['ENV']['KUROKO2_PARALLEL_FORK_INDEX']).to be_nil

subject.process_all
expect(Kuroko2::Token.all.count).to eq 0
end
Expand Down

0 comments on commit 78c67be

Please sign in to comment.