From: Klas Lindfors Date: Tue, 17 Dec 2013 11:07:42 +0000 (+0100) Subject: move the sleep in yk_wait_for_status() to the top of the loop X-Git-Tag: v1.14.2~6 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4657e7265d8984471607baebfb3ea74b804848c9;p=yubikey-personalization move the sleep in yk_wait_for_status() to the top of the loop fixes #38 it seems there is a race if a fast computer makes control transfers very close to each other in time, this enforces a 10ms sleep at the start of a read. --- diff --git a/ykcore/ykcore.c b/ykcore/ykcore.c index f129e5b..4810625 100644 --- a/ykcore/ykcore.c +++ b/ykcore/ykcore.c @@ -471,6 +471,13 @@ int yk_wait_for_key_status(YK_KEY *yk, uint8_t slot, unsigned int flags, slot = 0; while (slept_time < max_time_ms) { + Sleep(sleepval); + slept_time += sleepval; + /* exponential backoff, up to 500 ms */ + sleepval *= 2; + if (sleepval > 500) + sleepval = 500; + /* Read a status report from the key */ memset(data, 0, sizeof(data)); if (!_ykusb_read(yk, REPORT_TYPE_FEATURE, slot, (char *) &data, FEATURE_RPT_SIZE)) @@ -512,13 +519,6 @@ int yk_wait_for_key_status(YK_KEY *yk, uint8_t slot, unsigned int flags, break; } } - - Sleep(sleepval); - slept_time += sleepval; - /* exponential backoff, up to 500 ms */ - sleepval *= 2; - if (sleepval > 500) - sleepval = 500; } yk_errno = YK_ETIMEOUT;