Skip to content

servo: Allow configuring style stack size#269

Merged
mrobinson merged 1 commit intoservo:mainfrom
jschwe:style_thread_stack_size_configure
Jan 22, 2026
Merged

servo: Allow configuring style stack size#269
mrobinson merged 1 commit intoservo:mainfrom
jschwe:style_thread_stack_size_configure

Conversation

@jschwe
Copy link
Copy Markdown
Member

@jschwe jschwe commented Nov 15, 2025

When running Servo with ASAN we hit the stack limit. We can use compile-time parsing of environment variables, to allow the embedder to configure the style thread stack size.

Fixes #223

Comment on lines +40 to +51
pub const STYLE_THREAD_STACK_SIZE_KB: usize = const {
let default_stack_size = 512;
if let Some(user_def_size) = option_env!("SERVO_STYLE_THREAD_STACK_SIZE_KB") {
if let Ok(user_def_size) = usize::from_str_radix(user_def_size, 10) {
user_def_size
} else {
panic!("SERVO_STYLE_THREAD_STACK_SIZE_KB must be a valid integer")
}
} else {
default_stack_size
}
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to make this a bit simpler and just increase the stack size when ASAN is enabled?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given servo/servo#40814 I would also like to do this for debug builds, if possible.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer keeping it configurable too, since it allows embedders to adjust the stack size based on their needs.
The code is quite simple, just very verbose since we can't use any traits in const contexts, like and_then() or unwrap_or().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrobinson Coming back to this, could we revive the discussion here on how to continue?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given servo/servo#40814 I would also like to do this for debug builds, if possible.

We have already increased the size here just for Servo due to our debug builds. I guess we could increase it again. If possible, it would be nice to have the build just work, no matter the build configuration, without having to set a magic environment variable. Also, what are the implications of exposing this in production builds?

Yet...why do we need a larger stack size than Gecko?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, what are the implications of exposing this in production builds?

The environment variable is read at compile-time, so I don't see any issue there.

We have already increased the size here just for Servo due to our debug builds. I guess we could increase it again. If possible, it would be nice to have the build just work, no matter the build configuration, without having to set a magic environment variable.

As I understand, the whole point of this setting is to reduce the amount of memory the stylo threads use (since AFAIK the default is higher, I don't remember exactly but either 2 or 8 MB?). Increasing works, but it since release / production builds will require less memory due to optimizing, it would also be wasteful to just choose the highest option.

Yet...why do we need a larger stack size than Gecko?

I wonder if they perhaps always optimize stylo to a certain degree? (pure speculation)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also like to do this for debug builds, if possible.

The difficult thing here is that we can't detect that. We can only check debug-assertions, but we also enable those in release mode in servo so it's not optimal.

But I guess I could live with a cfg(any(debug_assertions, asan)), although I'm not a huge fan, if thats preferred.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difficult thing here is that we can't detect that. We can only check debug-assertions, but we also enable those in release mode in servo so it's not optimal.

Thinking about it again, we need to ensure that we run servo CI with the smaller stack size, to prevent sudden crashes in production. And since we do want to enable debug_assertions in servos CI, those two goals kind of exclude themselves. So we are back to the embedder build system somehow needing to pass the information to stylo, which could either be a cfg, which would be a global rustflag, a feature flag "debug", or an environment variable like the current one.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently, Gecko's default debug build does include some Rust optimizations. Maybe we could do that same.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently, Gecko's default debug build does include some Rust optimizations. Maybe we could do that same.

I'm not opposed to that. IMHO it might make sense to optimize all non-workspace dependencies in servo with 1 or perhaps even 2. It would slow down clean debug builds, improve the performance of debug builds, and perhaps slightly regress the ability to debug issues in non-workspace dependencies with gdb. Seems like a trade-off we could make if we document it properly in the book.

Anyway, we would probably still need to increase the stack size for asan, although a global cfg option for that would be fine, since we anyway need to set rustflags for asan.

Comment on lines +41 to +50
let default_stack_size = 512;
if let Some(user_def_size) = option_env!("SERVO_STYLE_THREAD_STACK_SIZE_KB") {
if let Ok(user_def_size) = usize::from_str_radix(user_def_size, 10) {
user_def_size
} else {
panic!("SERVO_STYLE_THREAD_STACK_SIZE_KB must be a valid integer")
}
} else {
default_stack_size
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can map be used here?

Copy link
Copy Markdown
Member Author

@jschwe jschwe Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure I tried that, and its not available in const contexts sadly.
Edit: docs.rs confirms map on option is still unstable in const contexts.

When running Servo with ASAN we hit the stack limit.
We can use compile-time parsing of environment variables,
to allow the embedder to configure the style thread stack size.
@jschwe jschwe force-pushed the style_thread_stack_size_configure branch from 7c96acc to 9f84f72 Compare January 20, 2026 10:13
@jschwe
Copy link
Copy Markdown
Member Author

jschwe commented Jan 22, 2026

FYI, I can't merge to this repo, so someone else would need to click the merge button if everything is alright otherwise.

@mrobinson mrobinson added this pull request to the merge queue Jan 22, 2026
Merged via the queue into servo:main with commit 9c1d70b Jan 22, 2026
5 checks passed
@jschwe jschwe deleted the style_thread_stack_size_configure branch January 22, 2026 14:07
jschwe added a commit to jschwe/servo that referenced this pull request Jan 28, 2026
Stylo servo/stylo#269 recently added the option
to configure the stack size of the style threads.
We increase the stack size for asan and debug builds.

Signed-off-by: Jonathan Schwender <[email protected]>
Loirooriol pushed a commit that referenced this pull request Feb 5, 2026
When running Servo with ASAN we hit the stack limit. We can use
compile-time parsing of environment variables, to allow the embedder to
configure the style thread stack size.

Fixes #223
Loirooriol pushed a commit that referenced this pull request Feb 5, 2026
When running Servo with ASAN we hit the stack limit. We can use
compile-time parsing of environment variables, to allow the embedder to
configure the style thread stack size.

Fixes #223
Copy link
Copy Markdown
Collaborator

@Loirooriol Loirooriol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of an env, wouldn't it be better to use style_config::get_i32() to get a pref that can be set from Servo? Like

style_config::get_i32("layout.threads")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow increasing stack size for use with ASAN

4 participants