Thread 105709230 - /g/ [Archived: 709 hours ago]

Anonymous
6/26/2025, 12:07:05 PM No.105709230
1720716026357153
1720716026357153
md5: ceb05c65f65798631464e0f8370a6262🔍
>Doesn't have native signal handling APIs
Oof. That's rough, rust friends.
Replies: >>105709414 >>105709528 >>105709557 >>105710195 >>105713632
Anonymous
6/26/2025, 12:39:01 PM No.105709414
>>105709230 (OP)
It has signal handling ability. It's called a match expression.
for sig in signals.forever() {
match sig {
SIGINT => {
println!("Received SIGINT (Ctrl+C). Shutting down...");
break;
}
SIGTERM => {
println!("Received SIGTERM. Shutting down...");
break;
}
_ => println!("Received unknown signal: {}", sig),
}
}
Replies: >>105709422 >>105709433
Anonymous
6/26/2025, 12:40:07 PM No.105709422
>>105709414
That's just a less readable swith.
Replies: >>105709428
Anonymous
6/26/2025, 12:41:20 PM No.105709428
>>105709422
If match expression is unreadable to you, consider a different hobby.
Anonymous
6/26/2025, 12:42:13 PM No.105709433
>>105709414
Ok, now register the signal handler :)
Replies: >>105709441
Anonymous
6/26/2025, 12:43:44 PM No.105709441
>>105709433
Sure :)
use signal_hook_registry::{register, unregister};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
// Shared flag to indicate shutdown should happen
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();

// Register handler for SIGINT (Ctrl+C)
let _handler_sigint = register(signal_hook_registry::SIGINT, move || {
println!("Received SIGINT. Shutting down...");
r.store(false, Ordering::SeqCst);
}).expect("Failed to register SIGINT handler");

// Register handler for SIGTERM
let _handler_sigterm = register(signal_hook_registry::SIGTERM, move || {
println!("Received SIGTERM. Shutting down...");
running.store(false, Ordering::SeqCst);
}).expect("Failed to register SIGTERM handler");

println!("Waiting for signals... (try Ctrl+C or sending SIGTERM)");

// Simulate work while checking the running flag
while running.load(Ordering::SeqCst) {
println!("Working...");
sleep(Duration::from_secs(1)).await;
}

println!("Goodbye!");
}
Replies: >>105709447 >>105709463 >>105711611
Anonymous
6/26/2025, 12:44:45 PM No.105709447
>>105709441
>third party libraries
>Needing tokio to handle signals
Haha, please kill yourself, ok? :)
Replies: >>105709462 >>105710964
Anonymous
6/26/2025, 12:46:54 PM No.105709462
>>105709447
>>third party libraries
What third party? signal hook is written in Rust
>Needing tokio to handle signals
No not really. To handle signals you only need a match expression.
>Haha, please kill yourself, ok? :)
No, are you mad? :)
Replies: >>105709470
Anonymous
6/26/2025, 12:47:04 PM No.105709463
1735202854339962
1735202854339962
md5: 260a2bba546d4edbf971b8a37c7c0248🔍
>>105709441
>yes, you need a whole async runtime to use a core os functionality in this blazingly fast systems programming language
Replies: >>105709468 >>105711485
Anonymous
6/26/2025, 12:48:02 PM No.105709468
>>105709463
Do you understand the concept of handling signals asynchronously?
Replies: >>105709497
Anonymous
6/26/2025, 12:48:17 PM No.105709470
>>105709462
Wow, you're so clever! Thanks for proving me right though :)
Replies: >>105709482
Anonymous
6/26/2025, 12:49:17 PM No.105709482
>>105709470
>Wow, you're so clever!
I know :)
>Thanks for proving me right t
Whatever lets you sleep at night :)
Anonymous
6/26/2025, 12:51:35 PM No.105709497
>>105709468
Is there any reason to slap on a runtime that's pretty much as large as Go's to achieve this task instead of just using threads from the stdlib?
Replies: >>105709509 >>105709518 >>105709552 >>105711029
Anonymous
6/26/2025, 12:52:44 PM No.105709509
>>105709497
Raw threads aren't virtual, you can use threadpools. But async makes sense too.
Anonymous
6/26/2025, 12:53:47 PM No.105709518
>>105709497
Yes, the reason is that Rust literally doesn't have signal handling APIs in the core lib, and there are pretty much no worthwhile signal handling crates that aren't async bloat. Your best bet is to just use libc directly.
Replies: >>105709554
Anonymous
6/26/2025, 12:54:32 PM No.105709528
>>105709230 (OP)
you posted this yesterday
Replies: >>105709536
Anonymous
6/26/2025, 12:55:06 PM No.105709536
>>105709528
I will post it tomorrow too, just so you know.
Replies: >>105709613
Anonymous
6/26/2025, 12:56:57 PM No.105709552
>>105709497
Crabs unironically believe it is undefined behavior having multiple threads listening for signals. Please understand.
Replies: >>105709569
Anonymous
6/26/2025, 12:57:15 PM No.105709554
>>105709518
>signal handling APIs in the core
core is meant to work without an OS. If you are handling signals, you already have an OS and thus it makes no sense to put it in the core lib :)
Replies: >>105709633
Anonymous
6/26/2025, 12:57:22 PM No.105709557
>>105709230 (OP)
>signal handling
Use case?
Anonymous
6/26/2025, 12:59:03 PM No.105709569
>>105709552
Sounds like you are hearing voices in your head.
Consider medication :)
Anonymous
6/26/2025, 1:05:36 PM No.105709613
>>105709536
Yeah you should try again. Your thread got train-wrecked today.
Replies: >>105709633
Anonymous
6/26/2025, 1:07:53 PM No.105709633
1750859757235418
1750859757235418
md5: fd83bda6d216e49545071d5fc387b3a3🔍
>>105709554
The standard library has file handling APIs. It also has several OS specific modules. There is no reason not to have signal handling APIs, even if its just a wrapper on signal.h. It also has several unsafe functions, so that isn't an excuse either.

>>105709613
>First rust post has to pull Tokio to achieve basic signal handling
:)
Replies: >>105709655
Anonymous
6/26/2025, 1:10:19 PM No.105709655
>>105709633
I don't see any tokio in first post :)
Replies: >>105709667
Anonymous
6/26/2025, 1:11:45 PM No.105709667
>>105709655
You don't see anything in the first post because that code doesn't even compile.
Replies: >>105709671
Anonymous
6/26/2025, 1:13:15 PM No.105709671
>>105709667
That code doest not compile because it does not setup the main function and signals iterator
But still no tokio tough :)
Replies: >>105709678
Anonymous
6/26/2025, 1:14:23 PM No.105709678
>>105709671
>Blah blah blah
Still doesn't compile though.
Replies: >>105709694
Anonymous
6/26/2025, 1:16:14 PM No.105709694
>>105709678
If you aren't able to setup the main function and the signals iterator, it's simply a skill issue :)
Replies: >>105709710
Anonymous
6/26/2025, 1:18:56 PM No.105709710
>>105709694
Register the signal handler and mask. Apparently your Tokio example wasn't it, so go on. Everyone's still waiting anon.
Replies: >>105709785
Anonymous
6/26/2025, 1:29:12 PM No.105709785
>>105709710
>Apparently your Tokio example wasn't it,
Who says so? :)
Anonymous
6/26/2025, 2:18:43 PM No.105710195
324325643
324325643
md5: c72b70d8facb2780507273b20848dc46🔍
>>105709230 (OP)
why does everything have to be so complicated in rustland?
Anonymous
6/26/2025, 2:21:08 PM No.105710218
1740937660748341
1740937660748341
md5: 2cb0217883307bb102bcf5633a300249🔍
That whole thread
Anonymous
6/26/2025, 4:06:15 PM No.105710964
>>105709447
You people keep talking about how you want to avoid tokio, you don't actually care about "bloat" do you? You just don't want to tokio because it's written by a trans woman, is that it?
Replies: >>105711008 >>105711041
Anonymous
6/26/2025, 4:10:29 PM No.105711008
>>105710964
This is why rust will never be taken seriously.
Replies: >>105711046
Anonymous
6/26/2025, 4:12:31 PM No.105711029
>>105709497
Yes, you won't understand it though, because you don't understand what async is
Anonymous
6/26/2025, 4:13:46 PM No.105711041
>>105710964
>you don't actually care about "bloat" do you?
Why wouldn't someone care about bloat?
>You just don't want to tokio because it's written because it's written by a trans woman, is that it?
Are you a troll? The original author is Carl Lerche, and he's not trans. I'm trans and never would have known that without looking it up.
Replies: >>105711061
Anonymous
6/26/2025, 4:14:20 PM No.105711046
>>105711008
>this is why
Why? Rust is moving forward with Tokio being the one true async runtime, it would even be intergrated into the stdlib if it weren't for the fact that they can't update stdlib as fast
Replies: >>105711056
Anonymous
6/26/2025, 4:15:49 PM No.105711056
>>105711046
Because tokio is bloat and async has inherent overhead, your shitty "futures" which is ironic, because rust has no future, not even singular one, are extra bloat with ZERO benefit.
Replies: >>105711081
Anonymous
6/26/2025, 4:16:30 PM No.105711061
>>105711041
>The original author is Carl Lerche
He rarely contributes nowadays into tokio itself, the current primary developer is trans
Replies: >>105711115
Anonymous
6/26/2025, 4:18:05 PM No.105711081
>>105711056
>rust has no future
What's gonna replace it then? Nobody wants to write C++ anymore
Anonymous
6/26/2025, 4:21:28 PM No.105711115
>>105711061
I doubt the reason someone who criticizes the use of Tokio as bloat is actually doing it because the main contributor is trans, though. Probably only someone very obsessed would even know and care that much. It might just be that they genuinely think it's bloated.
Anonymous
6/26/2025, 5:05:07 PM No.105711485
>>105709463
look ma
no async
use signal_hook_registry::register;
use std::time::Duration;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;

fn main() {
// Shared flag to indicate shutdown should happen
let running = Arc::new(AtomicBool::new(true));

unsafe {
let r = running.clone();
// Register handler for SIGINT (Ctrl+C)
let _handler_sigint = register(libc::SIGINT, move || {
println!("Received SIGINT. Shutting down...");
r.store(false, Ordering::SeqCst);
}).expect("Failed to register SIGINT handler");

let r = running.clone();
// Register handler for SIGTERM
let _handler_sigterm = register(libc::SIGTERM, move || {
println!("Received SIGTERM. Shutting down...");
r.store(false, Ordering::SeqCst);
}).expect("Failed to register SIGTERM handler");
}

println!("Waiting for signals... (try Ctrl+C or sending SIGTERM)");

// Simulate work while checking the running flag
while running.load(Ordering::SeqCst) {
println!("Working...");
thread::sleep(Duration::from_secs(1));
}

println!("Goodbye!");
}

But oh god IT'S UNSAFE!!!!
Replies: >>105711524 >>105711697
Anonymous
6/26/2025, 5:09:35 PM No.105711524
>>105711485
>libc::
Replies: >>105711600 >>105713632
Anonymous
6/26/2025, 5:17:20 PM No.105711600
>>105711524
s/libc::SIGINT/2/
Works the same way. This function just takes the an integer parameter.
Anonymous
6/26/2025, 5:18:47 PM No.105711611
>>105709441
this is retardedly convoluted to the point i'm hoping it's something an LLM shat.
neither async nor tokio are relevant for starters. but even if your main is async/tokio, you don't need more than something like this:
use signal_hook::consts::signal::*;
use futures_lite::stream::StreamExt;
use signal_hook_tokio::Signals;

async fn handle_signals(signals: Signals) {
let mut signals = signals.fuse();
while let Some(signal) = signals.next().await {
match signal {
SIGTERM | SIGINT | SIGQUIT => {
// handle code here
// exit process if you want
// std::process::exit(1);
},
_ => (), // ignored
}
}
}

#[tokio::main]
async fn main() -> YourResultType<()> {
let signals = Signals::new(&[SIGTERM, SIGINT, SIGQUIT])?;
let handle = signals.handle();
let signals_task = tokio::task::spawn(handle_signals(signals));

// main code
}
Replies: >>105711643
Anonymous
6/26/2025, 5:21:55 PM No.105711643
>>105711611 (Me)

and of course, after the main code:

// main code
handle.close();
signals_task.await?
Anonymous
6/26/2025, 5:27:31 PM No.105711697
>>105711485
You still have to pull signal_hook_registry. Is that maintained by the rust maintainers? If not, it's unreliable and misses the point. If it is I'll be happy.

I'm actually not against splitting basic STD functionality into different crates, provided they
1. Are actually referenced in the documentation so they're easy to find, and
2. Are maintaned by the same people who maintains the std lib, and not a random dude who will just burn out in two years and leave the whole thing hanging
Of course, actually explaining to a rustie why these things matter is impossible.
Replies: >>105711863
Anonymous
6/26/2025, 5:45:29 PM No.105711863
>>105711697
nta. but code is not a running machine that requires constant maintenance. in simple cases such these, if it works, it works. and it's not like libc implementations see a lot of development (besides plugging CVE's and finding new ways to make allocations slower in the name of "security").
maybe you get burned out by major compiler updates breaking code all the time, or maybe unexpected API and/or ABI breakage is always keeping you busy, but such experiences do not necessarily translate to other languages/ecosystems.
Anonymous
6/26/2025, 8:52:41 PM No.105713632
>>105709230 (OP)
Signal handling is not a universal feature of computers or operating systems, so it doesn't belong in the language. It belongs in a platform-specific library.

>>105711524
Yes, because those "signals" are a C feature defined in libc.