Skip to content

Commit

Permalink
KTOR-2644 Intercept continuation before adding to SFG state (#3265)
Browse files Browse the repository at this point in the history
* KTOR-2644 Intercept continuation before adding to SFG state

* KTOR-2644 Add SystemProperty to Disable SFG
  • Loading branch information
e5l committed Nov 29, 2022
1 parent dbfe54a commit 4279241
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
Expand Up @@ -7,7 +7,8 @@ package io.ktor.util.pipeline
import io.ktor.util.*
import kotlinx.coroutines.*
import kotlin.coroutines.*
import kotlin.jvm.*

internal expect val DISABLE_SFG: Boolean

/**
* Represents running execution of a pipeline
Expand Down Expand Up @@ -51,7 +52,7 @@ internal fun <TSubject : Any, TContext : Any> pipelineContextFor(
subject: TSubject,
coroutineContext: CoroutineContext,
debugMode: Boolean = false
): PipelineContext<TSubject, TContext> = if (debugMode) {
): PipelineContext<TSubject, TContext> = if (DISABLE_SFG || debugMode) {
DebugPipelineContext(context, interceptors, subject, coroutineContext)
} else {
SuspendFunctionGun(subject, context, interceptors)
Expand Down
Expand Up @@ -4,10 +4,7 @@

package io.ktor.util.pipeline

import io.ktor.util.CoroutineStackFrame
import io.ktor.util.StackTraceElement
import io.ktor.utils.io.*
import kotlinx.coroutines.*
import io.ktor.util.*
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*

Expand Down Expand Up @@ -76,7 +73,7 @@ internal class SuspendFunctionGun<TSubject : Any, TContext : Any>(
override suspend fun proceed(): TSubject = suspendCoroutineUninterceptedOrReturn { continuation ->
if (index == blocks.size) return@suspendCoroutineUninterceptedOrReturn subject

addContinuation(continuation)
addContinuation(continuation.intercepted())

if (loop(true)) {
discardLastRootContinuation()
Expand Down
7 changes: 7 additions & 0 deletions ktor-utils/js/src/io/ktor/util/pipeline/PipelineContext.js.kt
@@ -0,0 +1,7 @@
/*
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.util.pipeline

internal actual val DISABLE_SFG: Boolean = false
@@ -0,0 +1,8 @@
/*
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.util.pipeline

internal actual val DISABLE_SFG: Boolean =
System.getProperty("io.ktor.internal.disable.sfg") == "true"
@@ -0,0 +1,10 @@
/*
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.util.pipeline

import kotlinx.cinterop.*
import platform.posix.*

internal actual val DISABLE_SFG: Boolean = getenv("KTOR_INTERNAL_DISABLE_SFG")?.toKString() == "true"

0 comments on commit 4279241

Please sign in to comment.