Skip to content

[SR-11647] Swift 5.1 on Linux: Regression in OperationQueue concurrency #614

Open
@swift-ci

Description

@swift-ci
Previous ID SR-11647
Radar None
Original Reporter freak4pc (JIRA User)
Type Bug
Environment

Swift 5.1.1 latest Docker image

Additional Detail from JIRA
Votes 0
Component/s Foundation, libdispatch
Labels Bug, 5.1Regression
Assignee None
Priority Medium

md5: 4aa6a76a0b5ee28f3059851ef77c1f74

relates to:

  • SR-11864 Availability warnings in dead code paths

Issue Description:

We've bumped into an interesting regression in RxSwift's OperationQueueScheduler that wraps OperationQueue, specifically under Linux. It seems concurrency is broken or hangs.

The full issue is here:
ReactiveX/RxSwift#2077 (comment)

I was able to reduce the issue using only OperationQueue, without Rx:

import Foundation

let condition = NSCondition()
let sema = DispatchSemaphore(value: 0)
var writtenStarted = 0
var writtenEnded = 0
var events = [String]()
var lock = NSLock()

func performLocked(_ action: () -> Void) {
    lock.lock()
    action()
    lock.unlock()
}

let concurrent = {
    performLocked {
        print("Started")
        events.append("Started")
    }

    condition.lock()
    writtenStarted += 1
    condition.signal()
    while writtenStarted < 2 {
        condition.wait()
    }
    condition.unlock()

    performLocked {
        print("Ended")
        events.append("Ended")
    }

    condition.lock()
    writtenEnded += 1
    condition.signal()
    while writtenEnded < 2 {
        condition.wait()
    }
    condition.unlock()
    sema.signal()
}

let op1 = BlockOperation(block: concurrent)
let op2 = BlockOperation(block: concurrent)

let operationQueue = OperationQueue()
operationQueue.maxConcurrentOperationCount = 8

operationQueue.addOperation(op1)
operationQueue.addOperation(op2)
sema.wait()

Running the following code on macOS (in Swift 5.1 as well) or Linux with Swift 5.0 yields:

$ swift regression.swift
Started
Started
Ended
Ended

Running it under Linux with Swift 5.1:

$ swift -v
Swift version 5.1.1 (swift-5.1.1-RELEASE)

$ swift regression.swift
Started
-> At this point the process hangs and never completes

This seems to be an open regression in Swift 5.1 under Linux, but happy for any feedback or to provide any additional information.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions