Kotlin 2.0.0 - Suspension functions can only be called within coroutine body

I’m facing an issue with invoking weakReferenceTask, which represents a suspended function in Kotlin 1.9.24. Previously, I could call withContext or other suspension functions without any problems.

However, after updating to the new Kotlin release, I’m encountering the error message “Suspension functions can only be called within coroutine body.”

I’m unsure why this code, which worked perfectly in the previous release, is now causing issues. There are no editor errors, but when I remove the suspend keyword from weakReferenceTask: WeakReference<() -> Unit>, I then encounter an editor error stating that I cannot call withContext.

Could you please help me resolve this issue? I hope this explanation clarifies my problem.

fun loadStuff(){


    weakReferenceTask = WeakReference {

// The error comes at this stage with Kotlin 2.0.0 but in Kotlin 1.9.24 its fine

         withContext(dispatcherProvider.backgroundDispatcher) {
            // Stuff that I do

     }, priority = TaskPriority.LOAD_MORE_DATA_TASK.priority, tag = "loadMoreData"

// This is the function that I call

fun enqueuePriorityTask(

weakReferenceTask: WeakReference<suspend () -> Unit>,

priority: Int = TaskPriority.DEFAULT_TASK.priority,

insertionTime: Long? = null,

tag: String = ""


// This is active in my viewModel
private fun startTasksWithPrioritiesScope() {

    scopeWeakReference?.get()?.launch(backgroundDispatcher) {

        while (isActive) {

            try {

                // Stuff

                taskWithPriority?.apply {
                ongoingPriorityTask = TaskWithPriority(priority, weakReferenceTask, insertionTime, tag)
                weakReferenceTask.get()?.apply {
                hasTaskStarted = true

                // Stuff

        } catch (e: InterruptedException) {
                SmartLogger.e(loggerTag, message = { "startTasksWithPrioritiesScope - $e" })

Looks like a bug. Please file one at http://kotl.in/issue