Skip to content

Commit

Permalink
CELIX-272: fixed race conditions during shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetri committed Oct 19, 2015
1 parent 0e7da3c commit fabe667
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions framework/private/include/framework_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ struct framework {
celix_thread_cond_t dispatcher;
celix_thread_mutex_t dispatcherLock;
celix_thread_t dispatcherThread;
celix_thread_t shutdownThread;

framework_logger_pt logger;
};
Expand Down
25 changes: 13 additions & 12 deletions framework/private/src/framework.c
Original file line number Diff line number Diff line change
Expand Up @@ -2067,6 +2067,9 @@ celix_status_t framework_waitForStop(framework_pt framework) {
fw_log(framework->logger, OSGI_FRAMEWORK_LOG_ERROR, "Error unlocking the framework.");
return CELIX_FRAMEWORK_EXCEPTION;
}

celixThread_join(framework->shutdownThread, NULL);

fw_log(framework->logger, OSGI_FRAMEWORK_LOG_INFO, "FRAMEWORK: Successful shutdown");
return CELIX_SUCCESS;
}
Expand Down Expand Up @@ -2101,6 +2104,13 @@ static void *framework_shutdown(void *framework) {
hashMapIterator_destroy(iter);
celixThreadMutex_unlock(&fw->installedBundleMapLock);

err = celixThreadMutex_lock(&fw->mutex);
if (err != 0) {
fw_log(fw->logger, OSGI_FRAMEWORK_LOG_ERROR, "Error locking the framework, cannot exit clean.");
celixThread_exit(NULL);
return NULL;
}

if (celixThreadMutex_lock(&fw->dispatcherLock) != CELIX_SUCCESS) {
fw_log(fw->logger, OSGI_FRAMEWORK_LOG_ERROR, "Error locking the dispatcherThread.");
}
Expand All @@ -2118,12 +2128,7 @@ static void *framework_shutdown(void *framework) {
celixThread_join(fw->dispatcherThread, NULL);
}

err = celixThreadMutex_lock(&fw->mutex);
if (err != 0) {
fw_log(fw->logger, OSGI_FRAMEWORK_LOG_ERROR, "Error locking the framework, cannot exit clean.");
celixThread_exit(NULL);
return NULL;
}

err = celixThreadCondition_broadcast(&fw->shutdownGate);
if (err != 0) {
fw_log(fw->logger, OSGI_FRAMEWORK_LOG_ERROR, "Error waking the shutdown gate, cannot exit clean.");
Expand Down Expand Up @@ -2260,6 +2265,7 @@ static void *fw_eventDispatcher(void *fw) {
}

if (size == 0 && framework->shutdown) {
celixThreadMutex_unlock(&framework->dispatcherLock);
celixThread_exit(NULL);
return NULL;
}
Expand Down Expand Up @@ -2361,17 +2367,12 @@ static celix_status_t frameworkActivator_start(void * userData, bundle_context_p

static celix_status_t frameworkActivator_stop(void * userData, bundle_context_pt context) {
celix_status_t status = CELIX_SUCCESS;

celix_thread_t shutdownThread;
framework_pt framework;

if (bundleContext_getFramework(context, &framework) == CELIX_SUCCESS) {

fw_log(framework->logger, OSGI_FRAMEWORK_LOG_INFO, "FRAMEWORK: Start shutdownthread");
if (celixThread_create(&shutdownThread, NULL, &framework_shutdown, framework) == CELIX_SUCCESS) {
// celixThread_join(&status, shutdownThread);
celixThread_detach(shutdownThread);
} else {
if (celixThread_create(&framework->shutdownThread, NULL, &framework_shutdown, framework) != CELIX_SUCCESS) {
fw_log(framework->logger, OSGI_FRAMEWORK_LOG_ERROR, "Could not create shutdown thread, normal exit not possible.");
status = CELIX_FRAMEWORK_EXCEPTION;
}
Expand Down

0 comments on commit fabe667

Please sign in to comment.