Skip to content

Commit

Permalink
Fix net server stopping when handle is garbage collected
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Sep 25, 2023
1 parent a86a62a commit fbee7c8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fixed `net.serve` stopping when the returned `ServeHandle` is garbage collected
- Fixed missing trailing newline when using the `warn` global
- Fixed constructor for `CFrame` in the `roblox` built-in library not parsing the 12-arg overload correctly. ([#102])
- Fixed various functions for `CFrame` in the `roblox` built-in library being incorrect, specifically row-column ordering and some flipped signs. ([#103])
Expand Down
8 changes: 6 additions & 2 deletions src/lune/builtins/net/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tokio::sync::{mpsc, oneshot, Mutex};

use crate::lune::{
scheduler::Scheduler,
util::{traits::LuaEmitErrorExt, TableBuilder},
util::{futures::yield_forever, traits::LuaEmitErrorExt, TableBuilder},
};

use super::{
Expand Down Expand Up @@ -115,7 +115,11 @@ where
.http1_keepalive(true) // Web sockets must be kept alive
.serve(hyper_make_service)
.with_graceful_shutdown(async move {
shutdown_rx.recv().await;
if shutdown_rx.recv().await.is_none() {
// The channel was closed, meaning the serve handle
// was garbage collected by lua without being used
yield_forever().await;
}
});
if let Err(e) = result.await {
eprintln!("Net serve error: {e}")
Expand Down
18 changes: 18 additions & 0 deletions src/lune/util/futures.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

#[derive(Debug, Clone, Copy)]
pub struct YieldForever;

impl Future for YieldForever {
type Output = ();

fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Pending
}
}

pub fn yield_forever() -> YieldForever {
YieldForever
}
1 change: 1 addition & 0 deletions src/lune/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod table_builder;

pub mod formatting;
pub mod futures;
pub mod traits;

pub use table_builder::TableBuilder;

0 comments on commit fbee7c8

Please sign in to comment.