Skip to content

Commit

Permalink
pkey: resume key generation after interrupt
Browse files Browse the repository at this point in the history
Key/parameter generation (OpenSSL::PKey::*.{new,generate}) immediately
aborts when it is done with GVL released (in other words, no block is
given) and the thread is interrupted (e.g., by a signal) during the
operation.

Have ossl_generate_cb_2() acquire GVL and call rb_thread_check_ints()
if needed to process the pending interrupt rather than abort the
operation completely by returning 0.

Reference: https://bugs.ruby-lang.org/issues/14882
  • Loading branch information
rhenium committed Jul 2, 2018
1 parent 1f90516 commit 5daeb8b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions ext/openssl/ossl_pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ static ID id_private_q;
/*
* callback for generating keys
*/
void *
call_check_ints(void *arg)
{
rb_thread_check_ints();
return NULL;
}

int
ossl_generate_cb_2(int p, int n, BN_GENCB *cb)
{
Expand All @@ -38,19 +45,22 @@ ossl_generate_cb_2(int p, int n, BN_GENCB *cb)
*/
rb_protect(rb_yield, ary, &state);
if (state) {
arg->stop = 1;
arg->state = state;
return 0;
}
}
if (arg->stop) return 0;
if (arg->interrupted) {
arg->interrupted = 0;
rb_thread_call_with_gvl(call_check_ints, NULL);
}
return 1;
}

void
ossl_generate_cb_stop(void *ptr)
{
struct ossl_generate_cb_arg *arg = (struct ossl_generate_cb_arg *)ptr;
arg->stop = 1;
arg->interrupted = 1;
}

static void
Expand Down
2 changes: 1 addition & 1 deletion ext/openssl/ossl_pkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern const rb_data_type_t ossl_evp_pkey_type;

struct ossl_generate_cb_arg {
int yield;
int stop;
int interrupted;
int state;
};
int ossl_generate_cb_2(int p, int n, BN_GENCB *cb);
Expand Down

0 comments on commit 5daeb8b

Please sign in to comment.