Skip to content
Back to skills

Tokio Runtime

ASecurity

Async runtime for Rust with task scheduling, I/O, and timers.

  • 8 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added September 8, 2026
ai-agentsrustdebugging

Security analysis

A100/100

Pro scans all 4 files and shows the line behind each finding

Scanned September 8, 2026

npx -y skills add ngxtm/devkit --skill tokio-runtime --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Tokio Runtime?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Tokio Runtime
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ngxtm-tokio-runtime/badge)](https://www.skillsdirectory.com/skills/ngxtm-tokio-runtime)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: Tokio Runtime
description: Async runtime for Rust with task scheduling, I/O, and timers.
metadata:
  labels: [rust, tokio, async, runtime]
  triggers:
    files: ['**/main.rs', '**/lib.rs']
    keywords: [tokio, spawn, select, async, await]
---

# Tokio Runtime Standards

## Runtime Setup

```rust
// Full runtime with all features
#[tokio::main]
async fn main() {
    // Your async code
}

// Custom runtime configuration
#[tokio::main(flavor = "multi_thread", worker_threads = 4)]
async fn main() { }

// Current-thread runtime (single-threaded)
#[tokio::main(flavor = "current_thread")]
async fn main() { }
```

## Task Spawning

```rust
use tokio::task;

// Spawn async task
let handle = tokio::spawn(async {
    // Background work
    expensive_computation().await
});

// Wait for result
let result = handle.await?;

// Spawn blocking task (CPU-bound work)
let result = task::spawn_blocking(|| {
    // Blocking/CPU-intensive code
    compute_hash(data)
}).await?;

// Detached task (fire and forget)
tokio::spawn(async {
    log_event(event).await;
});
```

## Concurrency Patterns

```rust
use tokio::{select, join, time};

// Run multiple futures concurrently
let (a, b, c) = join!(task_a(), task_b(), task_c());

// Race futures, cancel losers
select! {
    result = fetch_primary() => handle_primary(result),
    result = fetch_backup() => handle_backup(result),
    _ = time::sleep(Duration::from_secs(5)) => timeout_error(),
}

// Timeout wrapper
let result = time::timeout(
    Duration::from_secs(30),
    long_running_task()
).await?;
```

## Synchronization

```rust
use tokio::sync::{Mutex, RwLock, mpsc, broadcast, oneshot};

// Async mutex (allows holding across await)
let data = Arc::new(Mutex::new(vec![]));
{
    let mut guard = data.lock().await;
    guard.push(item);
}

// RwLock for read-heavy workloads
let cache = Arc::new(RwLock::new(HashMap::new()));
let value = cache.read().await.get(&key);

// Channels
let (tx, mut rx) = mpsc::channel(100);
tx.send(message).await?;
while let Some(msg) = rx.recv().await {
    process(msg);
}

// Oneshot for single response
let (tx, rx) = oneshot::channel();
tokio::spawn(async move { tx.send(result) });
let response = rx.await?;
```

## I/O Operations

```rust
use tokio::{fs, io::{AsyncReadExt, AsyncWriteExt}, net::TcpListener};

// File I/O
let content = fs::read_to_string("file.txt").await?;
fs::write("output.txt", data).await?;

// TCP server
let listener = TcpListener::bind("127.0.0.1:8080").await?;
loop {
    let (socket, _) = listener.accept().await?;
    tokio::spawn(handle_connection(socket));
}

// Buffered I/O
use tokio::io::{BufReader, BufWriter};
let reader = BufReader::new(file);
let mut lines = reader.lines();
while let Some(line) = lines.next_line().await? {
    process(line);
}
```

## Graceful Shutdown

```rust
use tokio::signal;

async fn shutdown_signal() {
    let ctrl_c = async {
        signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
    };

    #[cfg(unix)]
    let terminate = async {
        signal::unix::signal(signal::unix::SignalKind::terminate())
            .expect("Failed to install SIGTERM handler")
            .recv()
            .await;
    };

    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();

    tokio::select! {
        _ = ctrl_c => {},
        _ = terminate => {},
    }
}
```

## Best Practices

1. **Never block**: Use `spawn_blocking` for CPU-bound or blocking I/O
2. **Channel sizing**: Size mpsc channels appropriately to avoid backpressure
3. **Error propagation**: Use `JoinHandle::await?` to propagate panics
4. **Timeouts**: Always add timeouts to network operations
5. **Tracing**: Use `tokio-console` for debugging async issues

Files in this skill

  • SKILL.md3.6 KB
  • references/REFERENCE.md451 B
  • references/async-patterns.md2.7 KB
  • references/synchronization.md3.1 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…