Skip to content

feat: add thread_stack_size params to customize the thread stack si… #3885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sqlx-sqlite/src/connection/establish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct EstablishParams {
busy_timeout: Duration,
statement_cache_capacity: usize,
log_settings: LogSettings,
pub(crate) thread_stack_size: usize,
extensions: IndexMap<CString, Option<CString>>,
pub(crate) thread_name: String,
pub(crate) command_channel_size: usize,
Expand Down Expand Up @@ -159,6 +160,7 @@ impl EstablishParams {
busy_timeout: options.busy_timeout,
statement_cache_capacity: options.statement_cache_capacity,
log_settings: options.log_settings.clone(),
thread_stack_size: options.thread_stack_size,
extensions,
thread_name: (options.thread_name)(thread_id as u64),
command_channel_size: options.command_channel_size,
Expand Down
1 change: 1 addition & 0 deletions sqlx-sqlite/src/connection/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl ConnectionWorker {

thread::Builder::new()
.name(params.thread_name.clone())
.stack_size(params.thread_stack_size)
.spawn(move || {
let (command_tx, command_rx) = flume::bounded(params.command_channel_size);

Expand Down
17 changes: 17 additions & 0 deletions sqlx-sqlite/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub struct SqliteConnectOptions {
pub(crate) log_settings: LogSettings,
pub(crate) immutable: bool,
pub(crate) vfs: Option<Cow<'static, str>>,
pub(crate) thread_stack_size: usize,

pub(crate) pragmas: IndexMap<Cow<'static, str>, Option<Cow<'static, str>>>,
/// Extensions are specified as a pair of \<Extension Name : Optional Entry Point>, the majority
Expand Down Expand Up @@ -191,6 +192,8 @@ impl SqliteConnectOptions {
// Soft limit on the number of rows that `ANALYZE` touches per index.
pragmas.insert("analysis_limit".into(), None);

let default_thread_stack_size = 512 * 1024; // 512KB
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not comfortable making the default stack size this small. Because we invoke user-supplied callbacks from the worker thread, it's impossible to say what a safe minimum stack size is besides the current default.

Additionally, there's no leeway for platform-specific requirements. 64-bit platforms are going to need more stack size than 32-bit platforms because the size of pointers and usize/isize values is doubled.

This should be Option<usize> and default to not specifying a stack size and just letting the std choose.


Self {
filename: Cow::Borrowed(Path::new(":memory:")),
in_memory: false,
Expand All @@ -202,6 +205,7 @@ impl SqliteConnectOptions {
log_settings: Default::default(),
immutable: false,
vfs: None,
thread_stack_size: default_thread_stack_size,
pragmas,
extensions: Default::default(),
collations: Default::default(),
Expand Down Expand Up @@ -230,6 +234,19 @@ impl SqliteConnectOptions {
&self.filename
}

/// Set the thread stack size in bytes.
///
/// The default thread stack size is 512KB.
pub fn thread_stack_size(mut self, size: usize) -> Self {
self.thread_stack_size = size;
self
}

/// Get the current thread stack size in bytes.
pub fn get_thread_stack_size(&self) -> usize {
self.thread_stack_size
}

/// Set the enforcement of [foreign key constraints](https://www.sqlite.org/pragma.html#pragma_foreign_keys).
///
/// SQLx chooses to enable this by default so that foreign keys function as expected,
Expand Down
Loading