Fix Ruby macOS - NSCFConstantString initialize error
Last night I decided to upgrade macOS to Monterey to use my iMac as an Apple Airplay receiver to watch the Udemy courses I downloaded on my iPad so that I can use my iPad to take notes. The installation took less than an hour in my 2019 iMac. But one thing I immediately noticed was a rake task failing that worked an hour before with a __NSCFConstantString initialize
error message.
The Error
rake very_good_security:curl_easy
Running via Spring preloader in process 13777
# .
# ..
# ...
objc[13777]: +[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called.
objc[13777]: +[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug.
The Solution
Step 1: If you are using Spring, like in a Rails project, stop it first.
spring stop
Step 2: Set a flag
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
Step 3: Run the code again. In my case it was as rake command:
rake very_good_security:curl_easy
Running via Spring preloader in process 13777
# .
# ..
# ...
# ... 💃🎉
Step 4: Now that you know it works, you can add it to your .bashrc
, .bashprofile
, or .zshrc
.
echo "\n# Fix NSCFConstantString initialize error\nexport OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES" >> ~/.zshrc
... or add this to the appropriate file:
# Fix NSCFConstantString initialize error
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
Now you are good to go.
Whats happening
I am still trying to figure out what's happening. I will update this section in the future when I find some free hours to dig deep. It's not just a Ruby thing as it also affects Python.
About the Author
Ziyan Junaideen -
Ziyan is an expert Ruby on Rails web developer with 8 years of experience specializing in SaaS applications. He spends his free time he writes blogs, drawing on his iPad, shoots photos.