Skip to content

Commit 25e79ce

Browse files
authored
fix(rust): Add warning about #[tokio::main] (#7633)
1 parent ba3da82 commit 25e79ce

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/platforms/rust/index.mdx

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ sentry = "{{@inject packages.version('sentry.rust') }}"
3232

3333
The most convenient way to use this library is the `sentry::init` function, which starts a Sentry client with a default set of integrations, and binds it to the current Hub.
3434

35-
The `sentry::init` function returns a guard that, when dropped, will flush Events that were not yet sent to the Sentry service. It has a two second deadline for this, so shutdown of applications might slightly delay as a result. Keeping the guard around or sending events will not work.
35+
The `sentry::init` function returns a guard that when dropped, will flush Events that weren't yet sent to Sentry. It has a two-second deadline, so application shutdown may be slightly delayed as a result. Be sure to keep the guard or you won't be able to send events.
3636

3737
<SignInNote />
3838

@@ -43,7 +43,44 @@ let _guard = sentry::init(("___PUBLIC_DSN___", sentry::ClientOptions {
4343
}));
4444
```
4545

46-
**Important:** Note your DSN. The DSN (Data Source Name) tells the SDK where to send events. If you forget it, view Settings -> Projects -> Client Keys (DSN) in sentry.io.
46+
**Important:** Remember your DSN. The DSN (Data Source Name) tells the SDK where to send events. If you forget it you can find it be going to: Settings -> Projects -> Client Keys (DSN) in sentry.io.
47+
48+
### Async Main Function
49+
50+
In a multithreaded application, spawned threads should inherit the Hub from the main thread. In order for that to happen, the Sentry client must be initialized before starting an async runtime or spawning threads. This means you'll have to avoid using macros such as `#[tokio::main]` or `#[actix_web::main]`, because they start the runtime first. So rather than doing this:
51+
52+
```rust {filename:main.rs}
53+
// WRONG
54+
#[tokio::main]
55+
async fn main() {
56+
let _guard = sentry::init(("___PUBLIC_DSN___", sentry::ClientOptions {
57+
release: sentry::release_name!(),
58+
..Default::default()
59+
}));
60+
61+
// implementation of main
62+
}
63+
```
64+
65+
Do this instead:
66+
67+
```rust {filename:main.rs}
68+
// RIGHT
69+
fn main() {
70+
let _guard = sentry::init(("___PUBLIC_DSN___", sentry::ClientOptions {
71+
release: sentry::release_name!(),
72+
..Default::default()
73+
}));
74+
75+
tokio::runtime::Builder::new_multi_thread()
76+
.enable_all()
77+
.build()
78+
.unwrap()
79+
.block_on(async {
80+
// implementation of main
81+
});
82+
}
83+
```
4784

4885
## Verify Setup
4986

0 commit comments

Comments
 (0)