cpuworker.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /* Copyright (c) 2003-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file cpuworker.c
  7. * \brief Uses the workqueue/threadpool code to farm CPU-intensive activities
  8. * out to subprocesses.
  9. *
  10. * The multithreading backend for this module is in workqueue.c; this module
  11. * specializes workqueue.c.
  12. *
  13. * Right now, we use this infrastructure
  14. * <ul><li>for processing onionskins in onion.c
  15. * <li>for compressing consensuses in consdiffmgr.c,
  16. * <li>and for calculating diffs and compressing them in consdiffmgr.c.
  17. * </ul>
  18. **/
  19. #include "core/or/or.h"
  20. #include "core/or/channel.h"
  21. #include "core/or/circuitbuild.h"
  22. #include "core/or/circuitlist.h"
  23. #include "core/or/connection_or.h"
  24. #include "app/config/config.h"
  25. #include "core/mainloop/cpuworker.h"
  26. #include "lib/crypt_ops/crypto_rand.h"
  27. #include "lib/crypt_ops/crypto_util.h"
  28. #include "core/or/onion.h"
  29. #include "feature/relay/onion_queue.h"
  30. #include "feature/stats/rephist.h"
  31. #include "feature/relay/router.h"
  32. #include "core/crypto/onion_crypto.h"
  33. #include "app/main/tor_threads.h"
  34. #include "lib/evloop/compat_libevent.h"
  35. #include "core/or/or_circuit_st.h"
  36. static void queue_pending_tasks(void);
  37. typedef struct worker_state_s {
  38. int generation;
  39. server_onion_keys_t *onion_keys;
  40. } worker_state_t;
  41. static void *
  42. worker_state_new(void *arg)
  43. {
  44. worker_state_t *ws;
  45. (void)arg;
  46. ws = tor_malloc_zero(sizeof(worker_state_t));
  47. ws->onion_keys = server_onion_keys_new();
  48. return ws;
  49. }
  50. #define worker_state_free(ws) \
  51. FREE_AND_NULL(worker_state_t, worker_state_free_, (ws))
  52. static void
  53. worker_state_free_(worker_state_t *ws)
  54. {
  55. if (!ws)
  56. return;
  57. server_onion_keys_free(ws->onion_keys);
  58. tor_free(ws);
  59. }
  60. static void
  61. worker_state_free_void(void *arg)
  62. {
  63. worker_state_free_(arg);
  64. }
  65. static replyqueue_t *replyqueue = NULL;
  66. static threadpool_t *threadpool = NULL;
  67. static int total_pending_tasks = 0;
  68. static int max_pending_tasks = 128;
  69. /** Initialize the cpuworker subsystem. It is OK to call this more than once
  70. * during Tor's lifetime.
  71. */
  72. void
  73. cpu_init(void)
  74. {
  75. if (!threadpool) {
  76. /*
  77. In our threadpool implementation, half the threads are permissive and
  78. half are strict (when it comes to running lower-priority tasks). So we
  79. always make sure we have at least two threads, so that there will be at
  80. least one thread of each kind.
  81. */
  82. const int n_threads = get_num_cpus(get_options()) + 1;
  83. threadpool = threadpool_new(n_threads,
  84. worker_state_new,
  85. worker_state_free_void,
  86. NULL,
  87. start_tor_thread);
  88. }
  89. if (!replyqueue) {
  90. replyqueue = replyqueue_new(0, threadpool);
  91. struct event_base *base = tor_libevent_get_base();
  92. int r = replyqueue_register_reply_event(replyqueue, base);
  93. tor_assert(r == 0);
  94. }
  95. /* Total voodoo. Can we make this more sensible? */
  96. max_pending_tasks = get_num_cpus(get_options()) * 64;
  97. }
  98. /** Shutdown the cpuworker subsystem, and wait for any threads to join. */
  99. void
  100. cpu_shutdown(void)
  101. {
  102. if (threadpool != NULL) {
  103. threadpool_shutdown(threadpool);
  104. threadpool = NULL;
  105. }
  106. // TODO: clean up the replyqueue
  107. }
  108. /** Magic numbers to make sure our cpuworker_requests don't grow any
  109. * mis-framing bugs. */
  110. #define CPUWORKER_REQUEST_MAGIC 0xda4afeed
  111. #define CPUWORKER_REPLY_MAGIC 0x5eedf00d
  112. /** A request sent to a cpuworker. */
  113. typedef struct cpuworker_request_t {
  114. /** Magic number; must be CPUWORKER_REQUEST_MAGIC. */
  115. uint32_t magic;
  116. /** Flag: Are we timing this request? */
  117. unsigned timed : 1;
  118. /** If we're timing this request, when was it sent to the cpuworker? */
  119. struct timeval started_at;
  120. /** A create cell for the cpuworker to process. */
  121. create_cell_t create_cell;
  122. /* Turn the above into a tagged union if needed. */
  123. } cpuworker_request_t;
  124. /** A reply sent by a cpuworker. */
  125. typedef struct cpuworker_reply_t {
  126. /** Magic number; must be CPUWORKER_REPLY_MAGIC. */
  127. uint32_t magic;
  128. /** True iff we got a successful request. */
  129. uint8_t success;
  130. /** Are we timing this request? */
  131. unsigned int timed : 1;
  132. /** What handshake type was the request? (Used for timing) */
  133. uint16_t handshake_type;
  134. /** When did we send the request to the cpuworker? */
  135. struct timeval started_at;
  136. /** Once the cpuworker received the request, how many microseconds did it
  137. * take? (This shouldn't overflow; 4 billion micoseconds is over an hour,
  138. * and we'll never have an onion handshake that takes so long.) */
  139. uint32_t n_usec;
  140. /** Output of processing a create cell
  141. *
  142. * @{
  143. */
  144. /** The created cell to send back. */
  145. created_cell_t created_cell;
  146. /** The keys to use on this circuit. */
  147. uint8_t keys[CPATH_KEY_MATERIAL_LEN];
  148. /** Input to use for authenticating introduce1 cells. */
  149. uint8_t rend_auth_material[DIGEST_LEN];
  150. } cpuworker_reply_t;
  151. typedef struct cpuworker_job_u {
  152. or_circuit_t *circ;
  153. union {
  154. cpuworker_request_t request;
  155. cpuworker_reply_t reply;
  156. } u;
  157. } cpuworker_job_t;
  158. static workqueue_reply_t
  159. update_state_threadfn(void *state_, void *work_)
  160. {
  161. worker_state_t *state = state_;
  162. worker_state_t *update = work_;
  163. server_onion_keys_free(state->onion_keys);
  164. state->onion_keys = update->onion_keys;
  165. update->onion_keys = NULL;
  166. worker_state_free(update);
  167. ++state->generation;
  168. return WQ_RPL_REPLY;
  169. }
  170. /** Called when the onion key has changed so update all CPU worker(s) with
  171. * new function pointers with which a new state will be generated.
  172. */
  173. void
  174. cpuworkers_rotate_keyinfo(void)
  175. {
  176. if (!threadpool) {
  177. /* If we're a client, then we won't have cpuworkers, and we won't need
  178. * to tell them to rotate their state.
  179. */
  180. return;
  181. }
  182. if (threadpool_queue_update(threadpool,
  183. worker_state_new,
  184. update_state_threadfn,
  185. worker_state_free_void,
  186. NULL)) {
  187. log_warn(LD_OR, "Failed to queue key update for worker threads.");
  188. }
  189. }
  190. /** Indexed by handshake type: how many onionskins have we processed and
  191. * counted of that type? */
  192. static uint64_t onionskins_n_processed[MAX_ONION_HANDSHAKE_TYPE+1];
  193. /** Indexed by handshake type, corresponding to the onionskins counted in
  194. * onionskins_n_processed: how many microseconds have we spent in cpuworkers
  195. * processing that kind of onionskin? */
  196. static uint64_t onionskins_usec_internal[MAX_ONION_HANDSHAKE_TYPE+1];
  197. /** Indexed by handshake type, corresponding to onionskins counted in
  198. * onionskins_n_processed: how many microseconds have we spent waiting for
  199. * cpuworkers to give us answers for that kind of onionskin?
  200. */
  201. static uint64_t onionskins_usec_roundtrip[MAX_ONION_HANDSHAKE_TYPE+1];
  202. /** If any onionskin takes longer than this, we clip them to this
  203. * time. (microseconds) */
  204. #define MAX_BELIEVABLE_ONIONSKIN_DELAY (2*1000*1000)
  205. /** Return true iff we'd like to measure a handshake of type
  206. * <b>onionskin_type</b>. Call only from the main thread. */
  207. static int
  208. should_time_request(uint16_t onionskin_type)
  209. {
  210. /* If we've never heard of this type, we shouldn't even be here. */
  211. if (onionskin_type > MAX_ONION_HANDSHAKE_TYPE)
  212. return 0;
  213. /* Measure the first N handshakes of each type, to ensure we have a
  214. * sample */
  215. if (onionskins_n_processed[onionskin_type] < 4096)
  216. return 1;
  217. /** Otherwise, measure with P=1/128. We avoid doing this for every
  218. * handshake, since the measurement itself can take a little time. */
  219. return crypto_fast_rng_one_in_n(get_thread_fast_rng(), 128);
  220. }
  221. /** Return an estimate of how many microseconds we will need for a single
  222. * cpuworker to process <b>n_requests</b> onionskins of type
  223. * <b>onionskin_type</b>. */
  224. uint64_t
  225. estimated_usec_for_onionskins(uint32_t n_requests, uint16_t onionskin_type)
  226. {
  227. if (onionskin_type > MAX_ONION_HANDSHAKE_TYPE) /* should be impossible */
  228. return 1000 * (uint64_t)n_requests;
  229. if (PREDICT_UNLIKELY(onionskins_n_processed[onionskin_type] < 100)) {
  230. /* Until we have 100 data points, just asssume everything takes 1 msec. */
  231. return 1000 * (uint64_t)n_requests;
  232. } else {
  233. /* This can't overflow: we'll never have more than 500000 onionskins
  234. * measured in onionskin_usec_internal, and they won't take anything near
  235. * 1 sec each, and we won't have anything like 1 million queued
  236. * onionskins. But that's 5e5 * 1e6 * 1e6, which is still less than
  237. * UINT64_MAX. */
  238. return (onionskins_usec_internal[onionskin_type] * n_requests) /
  239. onionskins_n_processed[onionskin_type];
  240. }
  241. }
  242. /** Compute the absolute and relative overhead of using the cpuworker
  243. * framework for onionskins of type <b>onionskin_type</b>.*/
  244. static int
  245. get_overhead_for_onionskins(uint32_t *usec_out, double *frac_out,
  246. uint16_t onionskin_type)
  247. {
  248. uint64_t overhead;
  249. *usec_out = 0;
  250. *frac_out = 0.0;
  251. if (onionskin_type > MAX_ONION_HANDSHAKE_TYPE) /* should be impossible */
  252. return -1;
  253. if (onionskins_n_processed[onionskin_type] == 0 ||
  254. onionskins_usec_internal[onionskin_type] == 0 ||
  255. onionskins_usec_roundtrip[onionskin_type] == 0)
  256. return -1;
  257. overhead = onionskins_usec_roundtrip[onionskin_type] -
  258. onionskins_usec_internal[onionskin_type];
  259. *usec_out = (uint32_t)(overhead / onionskins_n_processed[onionskin_type]);
  260. *frac_out = ((double)overhead) / onionskins_usec_internal[onionskin_type];
  261. return 0;
  262. }
  263. /** If we've measured overhead for onionskins of type <b>onionskin_type</b>,
  264. * log it. */
  265. void
  266. cpuworker_log_onionskin_overhead(int severity, int onionskin_type,
  267. const char *onionskin_type_name)
  268. {
  269. uint32_t overhead;
  270. double relative_overhead;
  271. int r;
  272. r = get_overhead_for_onionskins(&overhead, &relative_overhead,
  273. onionskin_type);
  274. if (!overhead || r<0)
  275. return;
  276. log_fn(severity, LD_OR,
  277. "%s onionskins have averaged %u usec overhead (%.2f%%) in "
  278. "cpuworker code ",
  279. onionskin_type_name, (unsigned)overhead, relative_overhead*100);
  280. }
  281. /** Handle a reply from the worker threads. */
  282. static void
  283. cpuworker_onion_handshake_replyfn(void *work_, workqueue_reply_t reply_status)
  284. {
  285. cpuworker_job_t *job = work_;
  286. cpuworker_reply_t rpl;
  287. or_circuit_t *circ = NULL;
  288. tor_assert(total_pending_tasks > 0);
  289. --total_pending_tasks;
  290. if (reply_status != WQ_RPL_REPLY) {
  291. goto done_processing;
  292. }
  293. /* Could avoid this, but doesn't matter. */
  294. memcpy(&rpl, &job->u.reply, sizeof(rpl));
  295. tor_assert(rpl.magic == CPUWORKER_REPLY_MAGIC);
  296. if (rpl.timed && rpl.success &&
  297. rpl.handshake_type <= MAX_ONION_HANDSHAKE_TYPE) {
  298. /* Time how long this request took. The handshake_type check should be
  299. needless, but let's leave it in to be safe. */
  300. struct timeval tv_end, tv_diff;
  301. int64_t usec_roundtrip;
  302. tor_gettimeofday(&tv_end);
  303. timersub(&tv_end, &rpl.started_at, &tv_diff);
  304. usec_roundtrip = ((int64_t)tv_diff.tv_sec)*1000000 + tv_diff.tv_usec;
  305. if (usec_roundtrip >= 0 &&
  306. usec_roundtrip < MAX_BELIEVABLE_ONIONSKIN_DELAY) {
  307. ++onionskins_n_processed[rpl.handshake_type];
  308. onionskins_usec_internal[rpl.handshake_type] += rpl.n_usec;
  309. onionskins_usec_roundtrip[rpl.handshake_type] += usec_roundtrip;
  310. if (onionskins_n_processed[rpl.handshake_type] >= 500000) {
  311. /* Scale down every 500000 handshakes. On a busy server, that's
  312. * less impressive than it sounds. */
  313. onionskins_n_processed[rpl.handshake_type] /= 2;
  314. onionskins_usec_internal[rpl.handshake_type] /= 2;
  315. onionskins_usec_roundtrip[rpl.handshake_type] /= 2;
  316. }
  317. }
  318. }
  319. circ = job->circ;
  320. log_debug(LD_OR,
  321. "Unpacking cpuworker reply %p, circ=%p, success=%d",
  322. job, circ, rpl.success);
  323. if (circ->base_.magic == DEAD_CIRCUIT_MAGIC) {
  324. /* The circuit was supposed to get freed while the reply was
  325. * pending. Instead, it got left for us to free so that we wouldn't freak
  326. * out when the job->circ field wound up pointing to nothing. */
  327. log_debug(LD_OR, "Circuit died while reply was pending. Freeing memory.");
  328. circ->base_.magic = 0;
  329. tor_free(circ);
  330. goto done_processing;
  331. }
  332. circ->workqueue_entry = NULL;
  333. if (TO_CIRCUIT(circ)->marked_for_close) {
  334. /* We already marked this circuit; we can't call it open. */
  335. log_debug(LD_OR,"circuit is already marked.");
  336. goto done_processing;
  337. }
  338. if (rpl.success == 0) {
  339. log_debug(LD_OR,
  340. "decoding onionskin failed. "
  341. "(Old key or bad software.) Closing.");
  342. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  343. goto done_processing;
  344. }
  345. if (onionskin_answer(circ,
  346. &rpl.created_cell,
  347. (const char*)rpl.keys, sizeof(rpl.keys),
  348. rpl.rend_auth_material) < 0) {
  349. log_warn(LD_OR,"onionskin_answer failed. Closing.");
  350. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  351. goto done_processing;
  352. }
  353. log_debug(LD_OR,"onionskin_answer succeeded. Yay.");
  354. done_processing:
  355. memwipe(&rpl, 0, sizeof(rpl));
  356. memwipe(job, 0, sizeof(*job));
  357. tor_free(job);
  358. queue_pending_tasks();
  359. }
  360. /** Implementation function for onion handshake requests. */
  361. static workqueue_reply_t
  362. cpuworker_onion_handshake_threadfn(void *state_, void *work_)
  363. {
  364. worker_state_t *state = state_;
  365. cpuworker_job_t *job = work_;
  366. /* variables for onion processing */
  367. server_onion_keys_t *onion_keys = state->onion_keys;
  368. cpuworker_request_t req;
  369. cpuworker_reply_t rpl;
  370. memcpy(&req, &job->u.request, sizeof(req));
  371. tor_assert(req.magic == CPUWORKER_REQUEST_MAGIC);
  372. memset(&rpl, 0, sizeof(rpl));
  373. const create_cell_t *cc = &req.create_cell;
  374. created_cell_t *cell_out = &rpl.created_cell;
  375. struct timeval tv_start = {0,0}, tv_end;
  376. int n;
  377. rpl.timed = req.timed;
  378. rpl.started_at = req.started_at;
  379. rpl.handshake_type = cc->handshake_type;
  380. if (req.timed)
  381. tor_gettimeofday(&tv_start);
  382. n = onion_skin_server_handshake(cc->handshake_type,
  383. cc->onionskin, cc->handshake_len,
  384. onion_keys,
  385. cell_out->reply,
  386. rpl.keys, CPATH_KEY_MATERIAL_LEN,
  387. rpl.rend_auth_material);
  388. if (n < 0) {
  389. /* failure */
  390. log_debug(LD_OR,"onion_skin_server_handshake failed.");
  391. memset(&rpl, 0, sizeof(rpl));
  392. rpl.success = 0;
  393. } else {
  394. /* success */
  395. log_debug(LD_OR,"onion_skin_server_handshake succeeded.");
  396. cell_out->handshake_len = n;
  397. switch (cc->cell_type) {
  398. case CELL_CREATE:
  399. cell_out->cell_type = CELL_CREATED; break;
  400. case CELL_CREATE2:
  401. cell_out->cell_type = CELL_CREATED2; break;
  402. case CELL_CREATE_FAST:
  403. cell_out->cell_type = CELL_CREATED_FAST; break;
  404. default:
  405. tor_assert(0);
  406. return WQ_RPL_SHUTDOWN;
  407. }
  408. rpl.success = 1;
  409. }
  410. rpl.magic = CPUWORKER_REPLY_MAGIC;
  411. if (req.timed) {
  412. struct timeval tv_diff;
  413. int64_t usec;
  414. tor_gettimeofday(&tv_end);
  415. timersub(&tv_end, &tv_start, &tv_diff);
  416. usec = ((int64_t)tv_diff.tv_sec)*1000000 + tv_diff.tv_usec;
  417. if (usec < 0 || usec > MAX_BELIEVABLE_ONIONSKIN_DELAY)
  418. rpl.n_usec = MAX_BELIEVABLE_ONIONSKIN_DELAY;
  419. else
  420. rpl.n_usec = (uint32_t) usec;
  421. }
  422. memcpy(&job->u.reply, &rpl, sizeof(rpl));
  423. memwipe(&req, 0, sizeof(req));
  424. memwipe(&rpl, 0, sizeof(req));
  425. return WQ_RPL_REPLY;
  426. }
  427. /** Take pending tasks from the queue and assign them to cpuworkers. */
  428. static void
  429. queue_pending_tasks(void)
  430. {
  431. or_circuit_t *circ;
  432. create_cell_t *onionskin = NULL;
  433. while (total_pending_tasks < max_pending_tasks) {
  434. circ = onion_next_task(&onionskin);
  435. if (!circ)
  436. return;
  437. if (assign_onionskin_to_cpuworker(circ, onionskin) < 0)
  438. log_info(LD_OR,"assign_to_cpuworker failed. Ignoring.");
  439. }
  440. }
  441. /** DOCDOC */
  442. MOCK_IMPL(workqueue_entry_t *,
  443. cpuworker_queue_work,(workqueue_priority_t priority,
  444. workqueue_reply_t (*fn)(void *, void *),
  445. void (*reply_fn)(void *, workqueue_reply_t),
  446. void *arg))
  447. {
  448. tor_assert(threadpool);
  449. tor_assert(replyqueue);
  450. return threadpool_queue_work_priority(threadpool,
  451. priority,
  452. fn,
  453. reply_fn,
  454. replyqueue,
  455. arg);
  456. }
  457. /** Try to tell a cpuworker to perform the public key operations necessary to
  458. * respond to <b>onionskin</b> for the circuit <b>circ</b>.
  459. *
  460. * Return 0 if we successfully assign the task, or -1 on failure.
  461. */
  462. int
  463. assign_onionskin_to_cpuworker(or_circuit_t *circ,
  464. create_cell_t *onionskin)
  465. {
  466. workqueue_entry_t *queue_entry;
  467. cpuworker_job_t *job;
  468. cpuworker_request_t req;
  469. int should_time;
  470. tor_assert(threadpool);
  471. tor_assert(replyqueue);
  472. if (!circ->p_chan) {
  473. log_info(LD_OR,"circ->p_chan gone. Failing circ.");
  474. tor_free(onionskin);
  475. return -1;
  476. }
  477. if (total_pending_tasks >= max_pending_tasks) {
  478. log_debug(LD_OR,"No idle cpuworkers. Queuing.");
  479. if (onion_pending_add(circ, onionskin) < 0) {
  480. tor_free(onionskin);
  481. return -1;
  482. }
  483. return 0;
  484. }
  485. if (!channel_is_client(circ->p_chan))
  486. rep_hist_note_circuit_handshake_assigned(onionskin->handshake_type);
  487. should_time = should_time_request(onionskin->handshake_type);
  488. memset(&req, 0, sizeof(req));
  489. req.magic = CPUWORKER_REQUEST_MAGIC;
  490. req.timed = should_time;
  491. memcpy(&req.create_cell, onionskin, sizeof(create_cell_t));
  492. tor_free(onionskin);
  493. if (should_time)
  494. tor_gettimeofday(&req.started_at);
  495. job = tor_malloc_zero(sizeof(cpuworker_job_t));
  496. job->circ = circ;
  497. memcpy(&job->u.request, &req, sizeof(req));
  498. memwipe(&req, 0, sizeof(req));
  499. ++total_pending_tasks;
  500. queue_entry = threadpool_queue_work_priority(threadpool,
  501. WQ_PRI_HIGH,
  502. cpuworker_onion_handshake_threadfn,
  503. cpuworker_onion_handshake_replyfn,
  504. replyqueue,
  505. job);
  506. if (!queue_entry) {
  507. log_warn(LD_BUG, "Couldn't queue work on threadpool");
  508. tor_free(job);
  509. return -1;
  510. }
  511. log_debug(LD_OR, "Queued task %p (qe=%p, circ=%p)",
  512. job, queue_entry, job->circ);
  513. circ->workqueue_entry = queue_entry;
  514. return 0;
  515. }
  516. /** If <b>circ</b> has a pending handshake that hasn't been processed yet,
  517. * remove it from the worker queue. */
  518. void
  519. cpuworker_cancel_circ_handshake(or_circuit_t *circ)
  520. {
  521. cpuworker_job_t *job;
  522. if (circ->workqueue_entry == NULL)
  523. return;
  524. job = workqueue_entry_cancel(circ->workqueue_entry);
  525. if (job) {
  526. /* It successfully cancelled. */
  527. memwipe(job, 0xe0, sizeof(*job));
  528. tor_free(job);
  529. tor_assert(total_pending_tasks > 0);
  530. --total_pending_tasks;
  531. /* if (!job), this is done in cpuworker_onion_handshake_replyfn. */
  532. circ->workqueue_entry = NULL;
  533. }
  534. }