neo3 1.3.0

Production-ready Rust SDK for Neo N3 blockchain with high-level API, unified error handling, and enterprise features
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
use std::{
	collections::{BTreeMap, HashMap},
	sync::{
		atomic::{AtomicU64, Ordering},
		Arc, Mutex,
	},
};

use futures_channel::{mpsc, oneshot};
use futures_util::{select_biased, FutureExt, StreamExt};
use primitive_types::U256;
use serde_json::value::{to_raw_value, RawValue};

#[cfg(not(target_arch = "wasm32"))]
use futures_util::future::Either;

use super::super::JsonRpcError;

#[cfg(not(target_arch = "wasm32"))]
use super::WebSocketConfig;
use super::{
	backend::{BackendDriver, WsBackend},
	ActiveSub, ConnectionDetails, InFlight, Instruction, Notification, PubSubItem, Response, SubId,
	WsClient, WsClientError,
};
#[cfg(not(target_arch = "wasm32"))]
use crate::config::NeoConstants;

pub(super) type SharedChannelMap =
	Arc<Mutex<HashMap<U256, mpsc::UnboundedReceiver<Box<RawValue>>>>>;

pub(super) const DEFAULT_RECONNECTS: usize = 5;

/// This struct manages the relationship between the u64 request ID, and U256
/// server-side subscription ID. It does this by aliasing the server ID to the
/// request ID, and returning the Request ID to the caller (hiding the server
/// ID in the SubscriptionManager internals.) Giving the caller a "fake"
/// subscription id allows the subscription to behave consistently across
/// reconnections
struct SubscriptionManager {
	// Active subs indexed by request id
	subs: BTreeMap<u64, ActiveSub>,
	// Maps active server-side IDs to local subscription IDs
	aliases: HashMap<U256, u64>,
	// Used to share notification channels with the WsClient(s)
	channel_map: SharedChannelMap,
}

impl SubscriptionManager {
	fn new(channel_map: SharedChannelMap) -> Self {
		Self { subs: Default::default(), aliases: Default::default(), channel_map }
	}

	fn reset_server_ids(&mut self) {
		self.aliases.clear();
		for sub in self.subs.values_mut() {
			sub.current_server_id = None;
		}
	}

	fn count(&self) -> usize {
		self.subs.len()
	}

	fn add_alias(&mut self, sub: U256, id: u64) {
		if let Some(entry) = self.subs.get_mut(&id) {
			entry.current_server_id = Some(sub);
		}
		self.aliases.insert(sub, id);
	}

	fn remove_alias(&mut self, server_id: U256) {
		if let Some(id) = self.aliases.get(&server_id) {
			if let Some(sub) = self.subs.get_mut(id) {
				sub.current_server_id = None;
			}
		}
		self.aliases.remove(&server_id);
	}

	#[tracing::instrument(skip(self))]
	fn end_subscription(&mut self, id: u64) -> Option<Box<RawValue>> {
		// Ensure any channel created during subscription establishment is cleaned up, even if
		// the request fails before the user calls `subscribe`.
		self.channel_map
			.lock()
			.unwrap_or_else(|e| e.into_inner())
			.remove(&U256::from(id));

		if let Some(sub) = self.subs.remove(&id) {
			if let Some(server_id) = sub.current_server_id {
				tracing::debug!(server_id = format!("0x{server_id:x}"), "Ending subscription");
				self.remove_alias(server_id);
				// drop the receiver as we don't need the result
				let (channel, _) = oneshot::channel();
				// Serialization errors are ignored, and result in the request
				// not being dispatched. This is fine, as worst case it will
				// result in the server sending us notifications we ignore
				let unsub_request = InFlight {
					method: "neo_unsubscribe".to_string(),
					params: SubId(server_id).serialize_raw().ok()?,
					channel,
					#[cfg(not(target_arch = "wasm32"))]
					deadline: None,
				};
				// reuse the RPC ID. this is somewhat dirty.
				return unsub_request.serialize_raw(id).ok();
			}
			tracing::trace!("No current server id");
		}
		tracing::trace!("Cannot end unknown subscription");
		None
	}

	#[tracing::instrument(skip_all, fields(server_id = ? notification.subscription))]
	fn handle_notification(&mut self, notification: Notification) {
		let server_id = notification.subscription;

		let Some(id) = self.aliases.get(&server_id).copied() else {
			tracing::debug!(
				server_id = format!("0x{server_id:x}"),
				"No aliased subscription found"
			);
			return;
		};

		let Some(active) = self.subs.get(&id) else {
			tracing::trace!(id, "Aliased subscription found, but not active");
			self.aliases.remove(&server_id);
			return;
		};

		tracing::debug!(id, "Forwarding notification to listener");
		// send the notification over the channel
		let send_res = active.channel.unbounded_send(notification.result);

		// receiver has dropped, so we drop the sub
		if send_res.is_err() {
			tracing::debug!(id, "Listener dropped. Dropping alias and subs");
			// Note: We remove the subscription mappings but don't send an unsubscribe request
			// to the server. This is because the channel receiver has already been dropped,
			// indicating the consumer is no longer interested in the subscription.
			// The server will continue sending notifications which we'll ignore.
			self.aliases.remove(&server_id);
			self.subs.remove(&id);
		}
	}

	fn req_success(&mut self, id: u64, result: Box<RawValue>) -> Box<RawValue> {
		if let Ok(server_id) = serde_json::from_str::<SubId>(result.get()) {
			tracing::debug!(id, server_id = %server_id.0, "Registering new sub alias");
			self.add_alias(server_id.0, id);
			let client_id = U256::from(id);
			match to_raw_value(&format!("0x{client_id:x}")) {
				Ok(raw) => raw,
				Err(e) => {
					// Best-effort: if we can't serialize the aliased subscription ID, fall back to
					// the server-provided subscription id instead of panicking.
					tracing::warn!(
						error = %e,
						id,
						"Failed to encode aliased subscription id; returning server id"
					);
					result
				},
			}
		} else {
			result
		}
	}

	fn has(&self, id: u64) -> bool {
		self.subs.contains_key(&id)
	}

	fn to_reissue(&self) -> impl Iterator<Item = (&u64, &ActiveSub)> {
		self.subs.iter()
	}

	fn service_subscription_request(
		&mut self,
		id: u64,
		params: Box<RawValue>,
	) -> Result<Box<RawValue>, WsClientError> {
		let (tx, rx) = mpsc::unbounded();

		let active_sub = ActiveSub { params, channel: tx, current_server_id: None };
		let req = active_sub.serialize_raw(id)?;

		// Explicit scope for the lock
		// This insertion should be made BEFORE the request returns.
		// So we make it before the request is even dispatched :)
		{
			self.channel_map.lock().unwrap_or_else(|e| e.into_inner()).insert(id.into(), rx);
		}
		self.subs.insert(id, active_sub);

		Ok(req)
	}
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
	use super::*;

	fn make_manager(timeout: core::time::Duration) -> RequestManager {
		let backend = BackendDriver::new_for_test();
		let conn = ConnectionDetails::new("ws://localhost:10334/ws", None);
		let (_instructions_tx, instructions) = mpsc::unbounded();

		RequestManager {
			id: AtomicU64::new(1),
			reconnects: 0,
			subs: SubscriptionManager::new(Default::default()),
			reqs: Default::default(),
			backend,
			conn,
			config: None,
			request_timeout: Some(timeout),
			instructions,
		}
	}

	#[tokio::test]
	async fn expires_timed_out_requests() {
		let mut manager = make_manager(core::time::Duration::from_millis(10));
		let (tx, rx) = oneshot::channel::<Response>();

		manager.reqs.insert(
			1,
			InFlight {
				method: "test_method".to_string(),
				params: to_raw_value(&()).unwrap(),
				channel: tx,
				deadline: Some(std::time::Instant::now() - core::time::Duration::from_secs(1)),
			},
		);

		manager.expire_timed_out_requests();

		let response = rx.await.unwrap();
		let err = response.unwrap_err();
		assert_eq!(err.code, -32000);
		assert!(err.message.contains("request timed out"));
		assert!(manager.reqs.is_empty());
	}

	#[tokio::test]
	async fn cleans_up_subscription_on_timeout() {
		let mut manager = make_manager(core::time::Duration::from_millis(10));

		let id = 7u64;
		let params = to_raw_value(&["newHeads"]).unwrap();
		manager.subs.service_subscription_request(id, params.clone()).unwrap();

		assert!(manager
			.subs
			.channel_map
			.lock()
			.unwrap_or_else(|e| e.into_inner())
			.contains_key(&U256::from(id)));

		let (tx, rx) = oneshot::channel::<Response>();
		manager.reqs.insert(
			id,
			InFlight {
				method: "neo_subscribe".to_string(),
				params,
				channel: tx,
				deadline: Some(std::time::Instant::now() - core::time::Duration::from_secs(1)),
			},
		);

		manager.expire_timed_out_requests();

		let response = rx.await.unwrap();
		assert!(response.is_err());
		assert!(!manager.subs.has(id));
		assert!(!manager
			.subs
			.channel_map
			.lock()
			.unwrap_or_else(|e| e.into_inner())
			.contains_key(&U256::from(id)));
	}
}

/// The `RequestManager` holds copies of all pending requests (as `InFlight`),
/// and active subscriptions (as `ActiveSub`). When reconnection occurs, all
/// pending requests are re-dispatched to the new backend, and all active subs
/// are re-subscribed
///
///  `RequestManager` holds a `BackendDriver`, to communicate with the current
/// backend. Reconnection is accomplished by instantiating a new `WsBackend` and
/// swapping out the manager's `BackendDriver`.
///
/// In order to provide continuity of subscription IDs to the client, the
/// `RequestManager` also keeps a `SubscriptionManager`. See the
/// `SubscriptionManager` docstring for more complete details
///
/// The behavior is accessed by the WsClient frontend, which implements ]
/// `JsonRpcClient`. The `WsClient` is cloneable, so no need for an arc :). It
/// communicates to the request manager via a channel, and receives
/// notifications in a shared map for the client to retrieve
///
/// The `RequestManager` shuts down and drops when all `WsClient` instances have
/// been dropped (because all instruction channel `UnboundedSender` instances
/// will have dropped).
pub(super) struct RequestManager {
	// Next JSON-RPC Request ID
	id: AtomicU64,
	// How many times we should reconnect the backend before erroring
	reconnects: usize,
	// Subscription manager
	subs: SubscriptionManager,
	// Requests for which a response has not been receivedc
	reqs: BTreeMap<u64, InFlight>,
	// Control of the active WS backend
	backend: BackendDriver,
	// The URL and optional auth info for the connection
	conn: ConnectionDetails,
	#[cfg(not(target_arch = "wasm32"))]
	// An Option wrapping a tungstenite WebsocketConfig. If None, the default config is used.
	config: Option<WebSocketConfig>,
	#[cfg(not(target_arch = "wasm32"))]
	request_timeout: Option<core::time::Duration>,
	// Instructions from the user-facing providers
	instructions: mpsc::UnboundedReceiver<Instruction>,
}

impl RequestManager {
	fn next_id(&mut self) -> u64 {
		self.id.fetch_add(1, Ordering::Relaxed)
	}

	pub(super) async fn connect(
		conn: ConnectionDetails,
	) -> Result<(Self, WsClient), WsClientError> {
		Self::connect_with_reconnects(conn, DEFAULT_RECONNECTS).await
	}

	async fn connect_internal(
		conn: ConnectionDetails,
	) -> Result<
		(
			BackendDriver,
			(mpsc::UnboundedSender<Instruction>, mpsc::UnboundedReceiver<Instruction>),
			SharedChannelMap,
		),
		WsClientError,
	> {
		let (ws, backend) = WsBackend::connect(conn).await?;

		ws.spawn();

		Ok((backend, mpsc::unbounded(), Default::default()))
	}

	#[cfg(target_arch = "wasm32")]
	pub(super) async fn connect_with_reconnects(
		conn: ConnectionDetails,
		reconnects: usize,
	) -> Result<(Self, WsClient), WsClientError> {
		let (backend, (instructions_tx, instructions_rx), channel_map) =
			Self::connect_internal(conn.clone()).await?;

		Ok((
			Self {
				id: Default::default(),
				reconnects,
				subs: SubscriptionManager::new(channel_map.clone()),
				reqs: Default::default(),
				backend,
				conn,
				instructions: instructions_rx,
			},
			WsClient { instructions: instructions_tx, channel_map },
		))
	}

	#[cfg(not(target_arch = "wasm32"))]
	pub(super) async fn connect_with_reconnects(
		conn: ConnectionDetails,
		reconnects: usize,
	) -> Result<(Self, WsClient), WsClientError> {
		let (backend, (instructions_tx, instructions_rx), channel_map) =
			Self::connect_internal(conn.clone()).await?;

		Ok((
			Self {
				id: Default::default(),
				reconnects,
				subs: SubscriptionManager::new(channel_map.clone()),
				reqs: Default::default(),
				backend,
				conn,
				config: None,
				request_timeout: NeoConstants::rpc_request_timeout(),
				instructions: instructions_rx,
			},
			WsClient { instructions: instructions_tx, channel_map },
		))
	}

	#[cfg(not(target_arch = "wasm32"))]
	pub(super) async fn connect_with_config(
		conn: ConnectionDetails,
		config: WebSocketConfig,
	) -> Result<(Self, WsClient), WsClientError> {
		Self::connect_with_config_and_reconnects(conn, config, DEFAULT_RECONNECTS).await
	}

	#[cfg(not(target_arch = "wasm32"))]
	pub(super) async fn connect_with_config_and_reconnects(
		conn: ConnectionDetails,
		config: WebSocketConfig,
		reconnects: usize,
	) -> Result<(Self, WsClient), WsClientError> {
		let (backend, (instructions_tx, instructions_rx), channel_map) =
			Self::connect_internal(conn.clone()).await?;

		Ok((
			Self {
				id: Default::default(),
				reconnects,
				subs: SubscriptionManager::new(channel_map.clone()),
				reqs: Default::default(),
				backend,
				conn,
				config: Some(config),
				request_timeout: NeoConstants::rpc_request_timeout(),
				instructions: instructions_rx,
			},
			WsClient { instructions: instructions_tx, channel_map },
		))
	}

	#[cfg(target_arch = "wasm32")]
	async fn reconnect_backend(&mut self) -> Result<(WsBackend, BackendDriver), WsClientError> {
		WsBackend::connect(self.conn.clone()).await
	}

	#[cfg(not(target_arch = "wasm32"))]
	async fn reconnect_backend(&mut self) -> Result<(WsBackend, BackendDriver), WsClientError> {
		if let Some(config) = self.config {
			WsBackend::connect_with_config(self.conn.clone(), config, false).await
		} else {
			WsBackend::connect(self.conn.clone()).await
		}
	}

	async fn reconnect(&mut self) -> Result<(), WsClientError> {
		if self.reconnects == 0 {
			return Err(WsClientError::TooManyReconnects);
		}
		self.reconnects -= 1;

		tracing::info!(remaining = self.reconnects, url = self.conn.url, "Reconnecting to backend");
		// create the new backend
		let (s, mut backend) = self.reconnect_backend().await?;

		// spawn the new backend
		s.spawn();

		// swap out the backend
		std::mem::swap(&mut self.backend, &mut backend);

		// rename for clarity
		let mut old_backend = backend;

		// Drain anything in the backend
		tracing::debug!("Draining old backend to_handle channel");
		while let Some(to_handle) = old_backend.to_handle.next().await {
			self.handle(to_handle);
		}

		// issue a shutdown command (even though it's likely gone)
		old_backend.shutdown();

		// Clear stale server-side subscription IDs before re-subscribing.
		self.subs.reset_server_ids();

		tracing::debug!(count = self.subs.count(), "Re-starting active subscriptions");
		let req_cnt = self.reqs.len();

		// reissue subscriptions
		for (id, sub) in self.subs.to_reissue() {
			let (tx, _rx) = oneshot::channel();
			let in_flight = InFlight {
				method: "neo_subscribe".to_string(),
				params: sub.params.clone(),
				channel: tx,
				#[cfg(not(target_arch = "wasm32"))]
				deadline: self.request_timeout.map(|timeout| std::time::Instant::now() + timeout),
			};
			// Need an entry in reqs to ensure response with new server sub ID is processed
			self.reqs.insert(*id, in_flight);
		}

		tracing::debug!(count = req_cnt, "Re-issuing pending requests");
		// reissue requests, including the re-subscription requests we just added above
		for (id, req) in self.reqs.iter() {
			self.backend
				.dispatcher
				.unbounded_send(req.serialize_raw(*id)?)
				.map_err(|_| WsClientError::DeadChannel)?;
		}
		tracing::info!(subs = self.subs.count(), reqs = req_cnt, "Re-connection complete");

		Ok(())
	}

	#[tracing::instrument(skip(self, result))]
	fn req_success(&mut self, id: u64, result: Box<RawValue>) {
		// pending fut is missing, this is fine
		tracing::trace!(len = result.get().len(), "Success response received");
		if let Some(req) = self.reqs.remove(&id) {
			tracing::debug!("Sending result to request listener");
			// Allow subscription manager to rewrite the result if the request
			// corresponds to a known ID
			let result = if self.subs.has(id) { self.subs.req_success(id, result) } else { result };
			let _ = req.channel.send(Ok(result));
		} else {
			tracing::trace!("No InFlight found");
		}
	}

	fn req_fail(&mut self, id: u64, error: JsonRpcError) {
		// pending fut is missing, this is fine
		if let Some(req) = self.reqs.remove(&id) {
			if self.subs.has(id) {
				let _ = self.subs.end_subscription(id);
			}
			// pending fut has been dropped, this is fine
			let _ = req.channel.send(Err(error));
		}
	}

	fn handle(&mut self, item: PubSubItem) {
		match item {
			PubSubItem::Success { id, result } => self.req_success(id, result),
			PubSubItem::Error { id, error } => self.req_fail(id, error),
			PubSubItem::Notification { params } => self.subs.handle_notification(params),
		}
	}

	#[tracing::instrument(skip(self, params, sender))]
	fn service_request(
		&mut self,
		id: u64,
		method: String,
		params: Box<RawValue>,
		sender: oneshot::Sender<Response>,
	) -> Result<(), WsClientError> {
		let in_flight = InFlight {
			method,
			params,
			channel: sender,
			#[cfg(not(target_arch = "wasm32"))]
			deadline: self.request_timeout.map(|timeout| std::time::Instant::now() + timeout),
		};
		let req = in_flight.serialize_raw(id)?;

		// Ordering matters here. We want this block above the unbounded send,
		// and after the serialization
		if in_flight.method == "neo_subscribe" {
			self.subs.service_subscription_request(id, in_flight.params.clone())?;
		}

		// Must come after self.subs.service_subscription_request. Do not re-order
		tracing::debug!("Dispatching request to backend");
		self.backend
			.dispatcher
			.unbounded_send(req)
			.map_err(|_| WsClientError::DeadChannel)?;

		self.reqs.insert(id, in_flight);
		Ok(())
	}

	#[cfg(not(target_arch = "wasm32"))]
	fn next_request_deadline(&self) -> Option<std::time::Instant> {
		self.reqs.values().filter_map(|req| req.deadline).min()
	}

	#[cfg(not(target_arch = "wasm32"))]
	fn expire_timed_out_requests(&mut self) {
		let Some(timeout) = self.request_timeout else {
			return;
		};

		let now = std::time::Instant::now();
		let mut expired_ids = Vec::new();
		for (id, req) in self.reqs.iter() {
			if req.deadline.is_some_and(|d| d <= now) {
				expired_ids.push(*id);
			}
		}

		for id in expired_ids {
			let Some(req) = self.reqs.remove(&id) else {
				continue;
			};

			if self.subs.has(id) {
				let _ = self.subs.end_subscription(id);
			}

			let _ = req.channel.send(Err(JsonRpcError {
				code: -32000,
				message: format!("request timed out after {timeout:?}: {}", req.method),
				data: None,
			}));
		}
	}

	fn service_instruction(&mut self, instruction: Instruction) -> Result<(), WsClientError> {
		match instruction {
			Instruction::Request { method, params, sender } => {
				let id = self.next_id();
				self.service_request(id, method, params, sender)?;
			},
			Instruction::Unsubscribe { id } => {
				if let Some(req) = self.subs.end_subscription(id.low_u64()) {
					self.backend
						.dispatcher
						.unbounded_send(req)
						.map_err(|_| WsClientError::DeadChannel)?;
				}
			},
		}
		Ok(())
	}

	pub(super) fn spawn(mut self) {
		let fut = async move {
			let result = loop {
				#[cfg(not(target_arch = "wasm32"))]
				self.expire_timed_out_requests();

				#[cfg(not(target_arch = "wasm32"))]
				let request_timeout = {
					let fut = if let Some(deadline) = self.next_request_deadline() {
						Either::Left(tokio::time::sleep_until(tokio::time::Instant::from_std(
							deadline,
						)))
					} else {
						Either::Right(futures_util::future::pending::<()>())
					};
					fut.fuse()
				};

				#[cfg(target_arch = "wasm32")]
				let request_timeout = futures_util::future::pending::<()>().fuse();

				futures_util::pin_mut!(request_timeout);

				// We bias the loop so that we always handle messages before
				// reconnecting, and always reconnect before dispatching new
				// requests
				select_biased! {
					item_opt = self.backend.to_handle.next() => {
						match item_opt {
							Some(item) => self.handle(item),
							// Backend is gone, so reconnect
							None => if let Err(e) = self.reconnect().await {
								break Err(e);
							}
						}
					},
					_ = &mut self.backend.error => {
						if let Err(e) = self.reconnect().await {
							break Err(e);
						}
					},
					_ = request_timeout => {
						#[cfg(not(target_arch = "wasm32"))]
						self.expire_timed_out_requests();
					},
					inst_opt = self.instructions.next() => {
						match inst_opt {
							Some(instruction) => if let Err(e) = self.service_instruction(instruction) { break Err(e)},
							// User-facing side is gone, so just exit
							None => break Ok(()),
						}
					}
				}
			};
			if let Err(err) = result {
				tracing::error!(%err, "Error during reconnection");
			}
			// Issue the shutdown command. we don't care if it is received
			self.backend.shutdown();
		};

		#[cfg(target_arch = "wasm32")]
		super::spawn_local(fut);

		#[cfg(not(target_arch = "wasm32"))]
		tokio::spawn(fut);
	}
}