Skip to content

Commit 5cc91f8

Browse files
committed
Merge feat/thread-stack-size-optional into main
2 parents e8384f2 + 6e73d87 commit 5cc91f8

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

sqlx-sqlite/src/connection/establish.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub struct EstablishParams {
2929
busy_timeout: Duration,
3030
statement_cache_capacity: usize,
3131
log_settings: LogSettings,
32+
pub(crate) thread_stack_size: Option<usize>,
3233
#[cfg(feature = "load-extension")]
3334
extensions: IndexMap<CString, Option<CString>>,
3435
pub(crate) thread_name: String,
@@ -142,6 +143,7 @@ impl EstablishParams {
142143
busy_timeout: options.busy_timeout,
143144
statement_cache_capacity: options.statement_cache_capacity,
144145
log_settings: options.log_settings.clone(),
146+
thread_stack_size: options.thread_stack_size,
145147
#[cfg(feature = "load-extension")]
146148
extensions,
147149
thread_name: (options.thread_name)(thread_id as u64),

sqlx-sqlite/src/connection/worker.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,14 @@ impl ConnectionWorker {
106106
pub(crate) async fn establish(params: EstablishParams) -> Result<Self, Error> {
107107
let (establish_tx, establish_rx) = oneshot::channel();
108108

109-
thread::Builder::new()
110-
.name(params.thread_name.clone())
111-
.spawn(move || {
109+
let mut builder = thread::Builder::new().name(params.thread_name.clone());
110+
111+
// Only set a custom stack size if explicitly configured
112+
if let Some(stack_size) = params.thread_stack_size {
113+
builder = builder.stack_size(stack_size);
114+
}
115+
116+
builder.spawn(move || {
112117
let (command_tx, command_rx) = flume::bounded(params.command_channel_size);
113118

114119
let conn = match params.establish() {

sqlx-sqlite/src/options/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub struct SqliteConnectOptions {
7070
pub(crate) log_settings: LogSettings,
7171
pub(crate) immutable: bool,
7272
pub(crate) vfs: Option<Cow<'static, str>>,
73+
pub(crate) thread_stack_size: Option<usize>,
7374

7475
pub(crate) pragmas: IndexMap<Cow<'static, str>, Option<Cow<'static, str>>>,
7576

@@ -204,6 +205,7 @@ impl SqliteConnectOptions {
204205
log_settings: Default::default(),
205206
immutable: false,
206207
vfs: None,
208+
thread_stack_size: None,
207209
pragmas,
208210
#[cfg(feature = "load-extension")]
209211
extensions: Default::default(),
@@ -233,6 +235,42 @@ impl SqliteConnectOptions {
233235
&self.filename
234236
}
235237

238+
/// Set the thread stack size in bytes for the SQLite worker thread.
239+
///
240+
/// **This is an advanced option.** By default (`None`), SQLx uses the Rust standard library's
241+
/// default stack size (typically 2 MB), which is safe for most use cases including user-supplied
242+
/// callbacks and platform-specific requirements.
243+
///
244+
/// Only set this if you have a specific reason to do so, such as running in an embedded environment
245+
/// with constrained memory. Be aware that:
246+
/// - User-supplied callbacks (hooks, custom functions) run on this thread and may have unpredictable
247+
/// stack requirements
248+
/// - Different platforms (32-bit vs 64-bit) have different stack size requirements
249+
/// - Setting this too low may cause stack overflow crashes
250+
///
251+
/// # Example
252+
///
253+
/// ```rust,no_run
254+
/// # use sqlx_sqlite::SqliteConnectOptions;
255+
/// # use std::str::FromStr;
256+
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
257+
/// let options = SqliteConnectOptions::from_str("sqlite::memory:")?
258+
/// .thread_stack_size(1024 * 1024); // 1 MB - use with caution!
259+
/// # Ok(())
260+
/// # }
261+
/// ```
262+
pub fn thread_stack_size(mut self, size: usize) -> Self {
263+
self.thread_stack_size = Some(size);
264+
self
265+
}
266+
267+
/// Get the current thread stack size in bytes.
268+
///
269+
/// Returns `None` if using the default stack size from the Rust standard library.
270+
pub fn get_thread_stack_size(&self) -> Option<usize> {
271+
self.thread_stack_size
272+
}
273+
236274
/// Set the enforcement of [foreign key constraints](https://www.sqlite.org/pragma.html#pragma_foreign_keys).
237275
///
238276
/// SQLx chooses to enable this by default so that foreign keys function as expected,

0 commit comments

Comments
 (0)