1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00
denoland-deno/cli/ops/timers.rs
Bert Belder 93b7acf99d
js: reschedule global timer if it fires earlier than expected (#2989)
When the global timer fires earlier than expected, which apparently
happens sometimes on server editions of Windows, we didn't call any
setTimeout callbacks, but we *also* didn't reschedule the global timer
to fire again later.

When this situation occurred it would make deno exit abruptly if there
were no other asynchronous ops running on the event loop. It could also
lead to application hangs if the upcoming setTimeout callback was
critical for the application to make progress.
2019-09-20 19:00:12 +02:00

42 lines
1 KiB
Rust

// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{Deserialize, JsonOp, Value};
use crate::state::ThreadSafeState;
use deno::*;
use futures::Future;
use std;
use std::time::Duration;
use std::time::Instant;
pub fn op_global_timer_stop(
state: &ThreadSafeState,
_args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let state = state;
let mut t = state.global_timer.lock().unwrap();
t.cancel();
Ok(JsonOp::Sync(json!({})))
}
#[derive(Deserialize)]
struct GlobalTimerArgs {
timeout: u64,
}
pub fn op_global_timer(
state: &ThreadSafeState,
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: GlobalTimerArgs = serde_json::from_value(args)?;
let val = args.timeout;
let state = state;
let mut t = state.global_timer.lock().unwrap();
let deadline = Instant::now() + Duration::from_millis(val);
let f = t
.new_timeout(deadline)
.then(move |_| futures::future::ok(json!({})));
Ok(JsonOp::Async(Box::new(f)))
}