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

**URL:** <https://discuss.kotlinlang.org/t/kotlin-2-0-0-suspension-functions-can-only-be-called-within-coroutine-body/28650>\
**Category:** Libraries\
**Created:** [May 21, 2024, 11:07pm UTC](https://discuss.kotlinlang.org/t/kotlin-2-0-0-suspension-functions-can-only-be-called-within-coroutine-body/28650 "2024-05-21T23:07:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vullnetlimani](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@vullnetlimani](https://discuss.kotlinlang.org/u/vullnetlimani)\
**Post date:** [May 21, 2024, 11:07pm UTC](https://discuss.kotlinlang.org/t/kotlin-2-0-0-suspension-functions-can-only-be-called-within-coroutine-body/28650/1 "2024-05-21T23:07:17Z")

</div>

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.

```auto
fun loadStuff(){

enqueuePriorityTask(

    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
             markTaskAsCompleted()
          }

     }, 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
                invoke()
                   }
                }

                // Stuff

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

```

---

<div class="post-metadata">

**Author:** ![ebrowne72](https://avatars.discourse-cdn.com/v4/letter/e/85f322/32.png) [@ebrowne72](https://discuss.kotlinlang.org/u/ebrowne72)\
**Post date:** [May 28, 2024, 5:00pm UTC](https://discuss.kotlinlang.org/t/kotlin-2-0-0-suspension-functions-can-only-be-called-within-coroutine-body/28650/2 "2024-05-28T17:00:44Z")

</div>

Looks like a bug. Please file one at [http://kotl.in/issue](http://kotl.in/issue)
