Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrency runtime never calls swift_task_enqueueMainExecutor_hook #63104

Open
MarSe32m opened this issue Jan 19, 2023 · 2 comments
Open

Concurrency runtime never calls swift_task_enqueueMainExecutor_hook #63104

MarSe32m opened this issue Jan 19, 2023 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. concurrency Feature: umbrella label for concurrency language features runtime The Swift Runtime

Comments

@MarSe32m
Copy link

Description
The hook swift_task_enqueueMainExecutor_hook is exported for users to implement custom main executor behavior but the concurrency runtime never calls this hook even though someone assigns a closure to it. On the other hand swift_task_enqueueGlobal_hook works as expected.

Steps to reproduce

swift_task_enqueueGlobal_hook = { job, original in 
    print("enqueueGlobal")
    original(job)
}

swift_task_enqueueMainExecutor_hook = { job, original in
    print("enqueueMainExecutor")
    original(job)
}

@MainActor
func someMainActorFunc() async {
    print("Main actor function")
}

Task { @MainActor in 
    print("Main actor task 1")
}

Task.detached { @MainActor in
    print("Main actor task 2")
}

Task.detached {
    print("Non main actor task")
}

await Task.detached {
    await someMainActorFunc()
}.value

To reproduce clone the package and swift run.

Expected behavior
It is expected that enqueueMainExecutor would be printed a couple of times.

Actual behavior
The output doesn't have any enqueueMainExecutors printed. The actual output is of the form:

enqueueGlobal
enqueueGlobal
enqueueGlobal
enqueueGlobal
Non main actor task
Main actor task 1
Main actor task 2
Main actor function
enqueueGlobal

Environment

  • Swift compiler version info: from 5.5 to 5.7.3 to nightly January 18, 2023
  • Platforms: macOS, Windows, Linux
@MarSe32m MarSe32m added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jan 19, 2023
@LucianoPAlmeida LucianoPAlmeida added concurrency Feature: umbrella label for concurrency language features runtime The Swift Runtime and removed triage needed This issue needs more specific labels labels Jan 20, 2023
@MarSe32m
Copy link
Author

Related forum thread.

@ephemer
Copy link

ephemer commented Jan 22, 2023

Your reply in the forum thread is important here. In particular the realisation that the current implementation of MainActor calls

func enqueue(_ job: UnownedJob) {
     _enqueueOnDispatchQueue(job, queue: self) 
}

(which doesn't provide any runtime hook), instead of:

public nonisolated func enqueue(_ job: UnownedJob) {
    _enqueueOnMain(job)
}

which appears to be what swift_task_enqueueMainExecutor_hook was implemented for initially.

One workaround would be to avoid the combination of swift_task_enqueueMainExecutor_hook with the DispatchGlobalExecutor and use the NonDispatchGlobalExecutor instead. We may indeed want to do that eventually (e.g. on Android), but for now it'd be helpful to only override MainActor invocations and leave the others to run on a global dispatch queue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. concurrency Feature: umbrella label for concurrency language features runtime The Swift Runtime
Projects
None yet
Development

No branches or pull requests

3 participants