Skip to content

Commit

Permalink
Merge pull request #756 from apache/hotfix/shutdown-hang
Browse files Browse the repository at this point in the history
Hotfix/Avoid scheduling event to an inactive framework.
  • Loading branch information
PengZheng committed Jun 24, 2024
2 parents f16b2d0 + 63445a3 commit 39e2282
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
23 changes: 23 additions & 0 deletions libs/framework/gtest/src/ScheduledEventTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "celix/FrameworkFactory.h"
#include "celix_bundle_context.h"
#include "celix_framework.h"
#include "celix_scheduled_event.h"

class ScheduledEventTestSuite : public ::testing::Test {
Expand Down Expand Up @@ -761,3 +762,25 @@ TEST_F(ScheduledEventTestSuite, ScheduledEventTimeoutLogTest) {
EXPECT_GE(logCount.load(), 2);
}
#endif

TEST_F(ScheduledEventTestSuite, ScheduledEventForInvactiveFramework) {
// Given a framework that is stopped
celix_framework_stopBundle(fw->getCFramework(), CELIX_FRAMEWORK_BUNDLE_ID);
celix_framework_waitForStop(fw->getCFramework());
// When a scheduled event is added
std::atomic<int> count{0};
auto callback = [](void* data) {
auto* count = static_cast<std::atomic<int>*>(data);
count->fetch_add(1);
};

celix_scheduled_event_options_t opts{};
opts.initialDelayInSeconds = 0.01;
opts.callbackData = &count;
opts.callback = callback;
long eventId = celix_bundleContext_scheduleEvent(fw->getFrameworkBundleContext()->getCBundleContext(), &opts);
EXPECT_LT(eventId, 0);

// Then the event is not added
EXPECT_EQ(0, count.load());
}
9 changes: 7 additions & 2 deletions libs/framework/src/framework.c
Original file line number Diff line number Diff line change
Expand Up @@ -2583,8 +2583,13 @@ long celix_framework_scheduleEvent(celix_framework_t* fw,
celix_bundleEntry_decreaseUseCount(bndEntry);

celixThreadMutex_lock(&fw->dispatcher.mutex);
celix_longHashMap_put(fw->dispatcher.scheduledEvents, id, event);
celixThreadCondition_broadcast(&fw->dispatcher.cond); //notify dispatcher thread for newly added scheduled event
if (fw->dispatcher.active) {
celix_longHashMap_put(fw->dispatcher.scheduledEvents, id, event);
celixThreadCondition_broadcast(&fw->dispatcher.cond); //notify dispatcher thread for newly added scheduled event
} else {
celix_scheduledEvent_release(event);
id = -1L;
}
celixThreadMutex_unlock(&fw->dispatcher.mutex);

return id;
Expand Down

0 comments on commit 39e2282

Please sign in to comment.