Fix segmentation fault when loading @tailwindcss/oxide in a Worker thread (#17276)

When Tailwind is loaded in a Node Worker thread, it currently causes a
segmentation fault on Linux when the thread exits. This is due to a
longstanding issue in Rust that affects all native modules:
https://github.com/rust-lang/rust/issues/91979. I reported this years
ago but unfortunately it is still not fixed, and seems to have gotten
worse in Rust 1.83.0 and later. Looks like Tailwind recently updated
Rust versions and this issue started appearing when run in tools like
Parcel that use worker threads.

The workaround is to prevent the native module from ever being unloaded.
One way to do that is to always load the native module in the main
thread in addition to workers, but this is hard to enforce.
@Brooooooklyn found another method, which is to use a linker option for
this. I tested this on an Ubuntu system and verified it fixed the issue.
You can test with the following script.

```js
// test.js
const {Worker} = require('worker_threads');
new Worker('./worker.js');

// worker.js
require('@tailwindcss/oxide');
```

Without this change, a segmentation fault will occur.

---------

Co-authored-by: Jordan Pittman <jordan@cryptica.me>
This commit is contained in:
Devon Govett 2025-03-18 13:28:20 -07:00 committed by GitHub
parent 1564bf092b
commit cec7f0557b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 38 additions and 0 deletions

View File

@ -30,6 +30,7 @@ jobs:
- vite
- cli
- postcss
- workers
# Exclude windows and macos from being built on feature branches
on-main-branch:

View File

@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Use the `oklab(…)` function when applying opacity to `currentColor` to work around a crash in Safari 16.4 and 16.5 ([#17247](https://github.com/tailwindlabs/tailwindcss/pull/17247))
- Pre-process `<template lang="…">` in Vue files ([#17252](https://github.com/tailwindlabs/tailwindcss/pull/17252))
- Remove redundant `line-height: initial` from Preflight ([#15212](https://github.com/tailwindlabs/tailwindcss/pull/15212))
- Prevent segfault when loaded in a worker thread on Linux ([#17276](https://github.com/tailwindlabs/tailwindcss/pull/17276))
## [4.0.14] - 2025-03-13

View File

@ -10,3 +10,5 @@ linker = "arm-linux-gnueabihf-gcc"
rustflags = ["-C", "target-feature=+crt-static"]
[target.aarch64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]
[target.'cfg(target_env = "gnu")']
rustflags = ["-C", "link-args=-Wl,-z,nodelete"]

View File

@ -0,0 +1,32 @@
import { js, json, test } from '../utils'
test(
'@tailwindcss/oxide can be loaded into a Node.js worker thread',
{
fs: {
'package.json': json`
{
"dependencies": {
"@tailwindcss/oxide": "workspace:^"
}
}
`,
'start.js': js`
let { Worker } = require('worker_threads')
new Worker('./worker.js')
`,
'worker.js': js`
require('@tailwindcss/oxide')
process.on('exit', () => console.log('worker thread exited'))
`,
},
},
async ({ exec, expect }) => {
let output = await exec('node ./start.js').then(
(out) => out.trim(),
(err) => `${err}`,
)
expect(output).toEqual('worker thread exited')
},
)

View File

@ -10,6 +10,7 @@
"./build.rs",
"./package.json",
"./Cargo.toml",
"./.cargo/config.toml",
"../oxide/src/**/*",
"../oxide/Cargo.toml",
"../Cargo.toml",
@ -24,6 +25,7 @@
"./build.rs",
"./package.json",
"./Cargo.toml",
"./.cargo/config.toml",
"../oxide/src/**/*",
"../oxide/Cargo.toml",
"../Cargo.toml",