Skip to content

Commit

Permalink
Bug#1194: delay thread cancellation during sqlite.
Browse files Browse the repository at this point in the history
If thread cancellation interrupts sqlite operation, the main thread may not be
able to acquire lock inside sqlite calls during clean up and hangs.

Change-Id: Iec9a50fff01aa5f0b24b56c3b7959929ab39c8ad
  • Loading branch information
nikhil-jain committed Sep 21, 2017
1 parent 3bd59f3 commit 36e6209
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/modules/content-sqlite/content-sqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ void load_cb (flux_t *h, flux_msg_handler_t *w,
int size = 0;
int uncompressed_size;
int rc = -1;
int old_state;
//delay cancellation to ensure lock-correctness in sqlite
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);

if (flux_request_decode_raw (msg, NULL, &blobref, &blobref_size) < 0) {
flux_log_error (h, "load: request decode failed");
Expand Down Expand Up @@ -316,6 +319,7 @@ void load_cb (flux_t *h, flux_msg_handler_t *w,
if (flux_respond_raw (h, msg, rc < 0 ? errno : 0, data, size) < 0)
flux_log_error (h, "load: flux_respond");
(void )sqlite3_reset (ctx->load_stmt);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
}

void store_cb (flux_t *h, flux_msg_handler_t *w,
Expand All @@ -328,6 +332,9 @@ void store_cb (flux_t *h, flux_msg_handler_t *w,
char blobref[BLOBREF_MAX_STRING_SIZE] = "-";
int uncompressed_size = -1;
int rc = -1;
int old_state;
//delay cancellation to ensure lock-correctness in sqlite
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);

if (flux_request_decode_raw (msg, NULL, &data, &size) < 0) {
flux_log_error (h, "store: request decode failed");
Expand Down Expand Up @@ -385,6 +392,7 @@ void store_cb (flux_t *h, flux_msg_handler_t *w,
blobref, strlen (blobref) + 1) < 0)
flux_log_error (h, "store: flux_respond");
(void) sqlite3_reset (ctx->store_stmt);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
}

int register_backing_store (flux_t *h, bool value, const char *name)
Expand Down Expand Up @@ -429,6 +437,7 @@ void shutdown_cb (flux_t *h, flux_msg_handler_t *w,
sqlite_ctx_t *ctx = arg;
flux_future_t *f;
int count = 0;
int old_state;

flux_log (h, LOG_DEBUG, "shutdown: begin");
if (register_backing_store (h, false, "content-sqlite") < 0) {
Expand All @@ -439,6 +448,8 @@ void shutdown_cb (flux_t *h, flux_msg_handler_t *w,
flux_log (h, LOG_DEBUG, "shutdown: instance is terminating, don't reload to cache");
goto done;
}
//delay cancellation to ensure lock-correctness in sqlite
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
while (sqlite3_step (ctx->dump_stmt) == SQLITE_ROW) {
const char *blobref;
int blobref_size;
Expand Down Expand Up @@ -496,6 +507,7 @@ void shutdown_cb (flux_t *h, flux_msg_handler_t *w,
(void )sqlite3_reset (ctx->dump_stmt);
flux_log (h, LOG_DEBUG, "shutdown: %d entries returned to cache", count);
done:
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
flux_reactor_stop (flux_get_reactor (h));
}

Expand Down

0 comments on commit 36e6209

Please sign in to comment.