-
Notifications
You must be signed in to change notification settings - Fork 55
Fix #1354 - Isolate variables in ForExecutor to avoid racing condition to overwrite loop variables #1363
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
base: main
Are you sure you want to change the base?
Fix #1354 - Isolate variables in ForExecutor to avoid racing condition to overwrite loop variables #1363
Changes from all commits
73f122e
e2e3746
7c2549b
2e95a6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Copyright 2020-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.serverlessworkflow.fluent.test; | ||
|
|
||
| import io.cloudevents.CloudEvent; | ||
| import io.serverlessworkflow.impl.events.InMemoryEvents; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.function.Consumer; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class LaggedInMemoryEvents extends InMemoryEvents { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(LaggedInMemoryEvents.class); | ||
|
|
||
| @Override | ||
| public CompletableFuture<Void> publish(CloudEvent ce) { | ||
| return CompletableFuture.runAsync( | ||
| () -> { | ||
| Consumer<CloudEvent> allConsumer = allConsumerRef.get(); | ||
| if (allConsumer != null) { | ||
| allConsumer.accept(ce); | ||
| } | ||
| Consumer<CloudEvent> consumer = topicMap.get(ce.getType()); | ||
| if (consumer != null) { | ||
| consumer.accept(ce); | ||
| } | ||
| try { | ||
| Thread.sleep(10); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new RuntimeException(e); | ||
| } | ||
| logger.info("Accepted event {} for topic {}", ce.getId(), ce.getType()); | ||
| }, | ||
| serviceFactory.get()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,14 +80,20 @@ protected CompletableFuture<WorkflowModel> internalExecute( | |
| CompletableFuture<WorkflowModel> future = | ||
| CompletableFuture.completedFuture(taskContext.input()); | ||
| while (iter.hasNext()) { | ||
| taskContext.variables().put(task.getFor().getEach(), iter.next()); | ||
| taskContext.variables().put(task.getFor().getAt(), i++); | ||
| final Object currentItem = iter.next(); | ||
| final int currentIndex = i++; | ||
| taskContext.variables().put(task.getFor().getEach(), currentItem); | ||
| taskContext.variables().put(task.getFor().getAt(), currentIndex); | ||
|
|
||
|
Comment on lines
+83
to
+87
Member
Author
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 is safe because |
||
| if (whileExpr.map(w -> w.test(workflow, taskContext, taskContext.input())).orElse(true)) { | ||
| future = | ||
| future.thenCompose( | ||
| input -> | ||
| TaskExecutorHelper.processTaskList( | ||
| taskExecutor, workflow, Optional.of(taskContext), input)); | ||
| input -> { | ||
| taskContext.variables().put(task.getFor().getEach(), currentItem); | ||
| taskContext.variables().put(task.getFor().getAt(), currentIndex); | ||
| return TaskExecutorHelper.processTaskList( | ||
| taskExecutor, workflow, Optional.of(taskContext), input); | ||
| }); | ||
| } else { | ||
| break; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.