Reference to var stdout is not concurrency-safe because it involves shared mutable state

I am writing a shell, within the shell I am using the fflush(stdout) function to output the last typed character to the prompt line while continuing to read the input.

This only becomes an issue when running on linux though, I am able to compile and run the same code fine on macOS.

To resolve the issue for the moment I call fflush(nil) to just flush all the available output streams, but I would rather do this another way if possible.

I am running this on swift 6.2.

Here is the repository and the line it was being used originally: Swell/Swell/IO/RawPromptHandler.swift at 559793ce9adc78dca615d932beee91211061c398 · MauriceElliott/Swell · GitHub and Swell/Swell/main.swift at 559793ce9adc78dca615d932beee91211061c398 · MauriceElliott/Swell · GitHub

This is how I’ve resolved it: Swell/Sources/IO/FlushWrapper.swift at main · MauriceElliott/Swell · GitHub and Swell/Sources/IO/PromptHandler.swift at 7df3e86ace13f9f73d95fd00f7f32facb6ba89dd · MauriceElliott/Swell · GitHub

Any other details required don’t hesistate to ask, if anyone that owns and runs linux on maybe just a supported distro (I am using Gentoo which isn’t technically supported by Swiftly?) could just confirm that references to stdout throw the same error that would be great.

Any support or advice in any regard would be helpful, so no pressure to provide answers, just want to know if I’m doing something completely wrong instead?

@preconcurrency import Foundation

1 Like

I just realised I missed describing the problem, any reference to stdout produces the error “Reference to stdout is not concurrency-safe because it involves shared mutable state”

Things I have tried that didn’t work:
nonisolated on a new variable that references stdout

An actor wrapper around referencing stdout

@preconcurrency on Foundation

All of the above still threw the error still. I can give the last on another go when I finish work today though just in case I didn’t use it everywhere I was referencing fflush.

you can try -strict-concurrency=minimal

example:QwiftUI/Package.swift at main · kkostov/QwiftUI · GitHub

1 Like

@kojirou Sorry for the slow reply, been super busy. The @preconcurrency on import Foundation did the trick, didn’t need the other part. Thanks for the extra information though!