Skip to content

Commit

Permalink
Merge pull request #42519 from lochana-chathura/master-fix-autoclose
Browse files Browse the repository at this point in the history
[Master] Fix autoClose() invocation when current strand is root strand
  • Loading branch information
lochana-chathura committed Apr 10, 2024
2 parents e72d652 + 860be97 commit e76d71b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.ballerina.runtime.internal.scheduling.Strand;
import io.ballerina.runtime.internal.scheduling.WorkerDataChannel;

import java.util.Objects;

/**
* Native implementation of lang.internal:WorkerChannels.
*
Expand All @@ -35,10 +37,11 @@ public class WorkerChannels {
* @param channelIds channel IDs of the channels to be closed
*/
public static void autoClose(BString[] channelIds) {
Strand parent = Scheduler.getStrand().parent;
Strand currentStrand = Scheduler.getStrand();
Strand channelHoldingStrand = Objects.requireNonNullElse(currentStrand.parent, currentStrand);
for (BString channelId : channelIds) {
String channelName = channelId.getValue() + ":" + (parent.functionInvocation - 1);
WorkerDataChannel workerDataChannel = parent.wdChannels.getWorkerDataChannel(channelName);
String channelName = channelId.getValue() + ":" + (channelHoldingStrand.functionInvocation - 1);
WorkerDataChannel workerDataChannel = channelHoldingStrand.wdChannels.getWorkerDataChannel(channelName);
workerDataChannel.autoClose();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public void testForkWithWorkersInSameFunction() {
long returnInt = (long) returns;
Assert.assertEquals(returnInt, 10);
}

@Test(description = "Test fork within if condition")
public void testForkWithinIfCondition() {
CompileResult result = BCompileUtil.compile("test-src/workers/fork-within-if-condition.bal");
Object returns = BRunUtil.invoke(result, "testForkWithinIfCondition");
Assert.assertTrue(returns instanceof Long);
Assert.assertEquals(((Long) returns).intValue(), 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com).
//
// WSO2 LLC. licenses this file to you 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.

function testForkWithinIfCondition() returns int {
boolean isForked = false;
if isForked {
fork {
worker A {
5 -> B;
string value = <- B;
}

worker B {
int value = <- A;
"a" -> A;
}
}
return 0;
}
return 1;
}

0 comments on commit e76d71b

Please sign in to comment.