You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@livy.apache.org by "nileshrathi345 (via GitHub)" <gi...@apache.org> on 2026/08/04 10:03:03 UTC
[PR] [LIVY-1063] Fix resource leak in Kubernetes service account token refresh [livy]
nileshrathi345 opened a new pull request, #536:
URL: https://github.com/apache/livy/pull/536
## What changes were proposed in this pull request?
Reuse the existing `KubernetesClient` during service account token refresh instead of
recreating `DefaultKubernetesClient` every 5 minutes without closing the old client.
JIRA: https://issues.apache.org/jira/browse/LIVY-1063
## How was this patch tested?
- Existing unit tests: `mvn -pl server -am -DskipITs test`
- Manual review: refresh loop only calls `config.setOauthToken()`; no new client creation
- Manual testing: deployed on Kubernetes with in-cluster SA auth;
verified "Refreshed Kubernetes service account token." logs every ~5 minutes and
native/HTTP client thread count remains stable over several refresh cycles
## Was this patch authored or co-authored using generative AI tooling?
No
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@livy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [LIVY-1063] Fix resource leak in Kubernetes service account token refresh [livy]
Posted by "nileshrathi345 (via GitHub)" <gi...@apache.org>.
nileshrathi345 commented on code in PR #536:
URL: https://github.com/apache/livy/pull/536#discussion_r3733253708
##########
server/src/main/scala/org/apache/livy/utils/SparkKubernetesApp.scala:
##########
@@ -78,25 +78,24 @@ object SparkKubernetesApp extends Logging {
val RefreshServiceAccountTokenThread = new Thread() {
override def run(): Unit = {
- while (true) {
- var currentContext = new Context()
- var currentContextName = new String
- val config = kubernetesClient.getConfiguration
- if (config.getCurrentContext != null) {
- currentContext = config.getCurrentContext.getContext
- currentContextName = config.getCurrentContext.getName
+ while (!Thread.currentThread().isInterrupted) {
+ try {
+ val config = kubernetesClient.getConfiguration
+ val contextName = Option(config.getCurrentContext).map(_.getName).getOrElse("")
+ val newestConfig = Config.autoConfigure(contextName)
+ config.setOauthToken(newestConfig.getOauthToken)
+ info("Refreshed Kubernetes service account token.")
+
+ // Token will expire 1 hour default, community recommend to update every 5 minutes
+ Thread.sleep(300000)
+ } catch {
+ case e: InterruptedException =>
+ Thread.currentThread().interrupt()
+ warn("RefreshServiceAccountTokenThread was interrupted, exiting.", e)
+ return
+ case NonFatal(e) =>
+ warn(s"Failed to refresh Kubernetes service account token, will retry.", e)
Review Comment:
Valid point — without a delay, a persistent failure would retry in a tight loop. I've added a 5-minute sleep in the NonFatal path, same as the normal refresh interval, before retrying.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@livy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [LIVY-1063] Fix resource leak in Kubernetes service account token refresh [livy]
Posted by "nileshrathi345 (via GitHub)" <gi...@apache.org>.
nileshrathi345 commented on code in PR #536:
URL: https://github.com/apache/livy/pull/536#discussion_r3821754079
##########
server/src/main/scala/org/apache/livy/utils/SparkKubernetesApp.scala:
##########
@@ -77,26 +77,36 @@ object SparkKubernetesApp extends Logging {
}
val RefreshServiceAccountTokenThread = new Thread() {
+ // Reuse the KubernetesClient created at startup; refresh its OAuth token in place
+ private val tokenRefreshIntervalMs = 300000L
+
override def run(): Unit = {
- while (true) {
- var currentContext = new Context()
Review Comment:
Thanks @roczei for suggestion— extracted the token refresh logic into refreshServiceAccountToken and added unit tests in SparkKubernetesAppSpec to verify the OAuth token is updated on the existing client config
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@livy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [LIVY-1063] Fix resource leak in Kubernetes service account token refresh [livy]
Posted by "gyogal (via GitHub)" <gi...@apache.org>.
gyogal merged PR #536:
URL: https://github.com/apache/livy/pull/536
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@livy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [LIVY-1063] Fix resource leak in Kubernetes service account token refresh [livy]
Posted by "nileshrathi345 (via GitHub)" <gi...@apache.org>.
nileshrathi345 commented on code in PR #536:
URL: https://github.com/apache/livy/pull/536#discussion_r3821754079
##########
server/src/main/scala/org/apache/livy/utils/SparkKubernetesApp.scala:
##########
@@ -77,26 +77,36 @@ object SparkKubernetesApp extends Logging {
}
val RefreshServiceAccountTokenThread = new Thread() {
+ // Reuse the KubernetesClient created at startup; refresh its OAuth token in place
+ private val tokenRefreshIntervalMs = 300000L
+
override def run(): Unit = {
- while (true) {
- var currentContext = new Context()
Review Comment:
Thanks @roczei for suggestion — extracted the token refresh logic into refreshServiceAccountToken and added unit tests in SparkKubernetesAppSpec to verify the OAuth token is updated on the existing client config
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@livy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [LIVY-1063] Fix resource leak in Kubernetes service account token refresh [livy]
Posted by "gyogal (via GitHub)" <gi...@apache.org>.
gyogal commented on code in PR #536:
URL: https://github.com/apache/livy/pull/536#discussion_r3730017461
##########
server/src/main/scala/org/apache/livy/utils/SparkKubernetesApp.scala:
##########
@@ -78,25 +78,24 @@ object SparkKubernetesApp extends Logging {
val RefreshServiceAccountTokenThread = new Thread() {
override def run(): Unit = {
- while (true) {
- var currentContext = new Context()
- var currentContextName = new String
- val config = kubernetesClient.getConfiguration
- if (config.getCurrentContext != null) {
- currentContext = config.getCurrentContext.getContext
- currentContextName = config.getCurrentContext.getName
+ while (!Thread.currentThread().isInterrupted) {
+ try {
+ val config = kubernetesClient.getConfiguration
+ val contextName = Option(config.getCurrentContext).map(_.getName).getOrElse("")
+ val newestConfig = Config.autoConfigure(contextName)
+ config.setOauthToken(newestConfig.getOauthToken)
+ info("Refreshed Kubernetes service account token.")
+
+ // Token will expire 1 hour default, community recommend to update every 5 minutes
+ Thread.sleep(300000)
+ } catch {
+ case e: InterruptedException =>
+ Thread.currentThread().interrupt()
+ warn("RefreshServiceAccountTokenThread was interrupted, exiting.", e)
+ return
+ case NonFatal(e) =>
+ warn(s"Failed to refresh Kubernetes service account token, will retry.", e)
Review Comment:
I was wondering in case there is a NonFatal exception, shouldn't there be a delay before retrying? If an exception is thrown during token refresh, it may be spinning in the loop too fast... is this something that can happen?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@livy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [LIVY-1063] Fix resource leak in Kubernetes service account token refresh [livy]
Posted by "nileshrathi345 (via GitHub)" <gi...@apache.org>.
nileshrathi345 commented on code in PR #536:
URL: https://github.com/apache/livy/pull/536#discussion_r3821754079
##########
server/src/main/scala/org/apache/livy/utils/SparkKubernetesApp.scala:
##########
@@ -77,26 +77,36 @@ object SparkKubernetesApp extends Logging {
}
val RefreshServiceAccountTokenThread = new Thread() {
+ // Reuse the KubernetesClient created at startup; refresh its OAuth token in place
+ private val tokenRefreshIntervalMs = 300000L
+
override def run(): Unit = {
- while (true) {
- var currentContext = new Context()
Review Comment:
Thanks @roczei for suggestion — extracted the token refresh logic into refreshServiceAccountToken and added unit tests to verify the OAuth token is updated on the existing client config
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@livy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [LIVY-1063] Fix resource leak in Kubernetes service account token refresh [livy]
Posted by "nileshrathi345 (via GitHub)" <gi...@apache.org>.
nileshrathi345 commented on PR #536:
URL: https://github.com/apache/livy/pull/536#issuecomment-5177530616
@gyogal and @roczei could please take a look when possible and review.
thanks..!!!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@livy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [LIVY-1063] Fix resource leak in Kubernetes service account token refresh [livy]
Posted by "roczei (via GitHub)" <gi...@apache.org>.
roczei commented on code in PR #536:
URL: https://github.com/apache/livy/pull/536#discussion_r3814133176
##########
server/src/main/scala/org/apache/livy/utils/SparkKubernetesApp.scala:
##########
@@ -77,26 +77,36 @@ object SparkKubernetesApp extends Logging {
}
val RefreshServiceAccountTokenThread = new Thread() {
+ // Reuse the KubernetesClient created at startup; refresh its OAuth token in place
+ private val tokenRefreshIntervalMs = 300000L
+
override def run(): Unit = {
- while (true) {
- var currentContext = new Context()
Review Comment:
Hi @nileshrathi345
It'd be good to validate this with a unit test. Could you maybe add a new test for it?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@livy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [LIVY-1063] Fix resource leak in Kubernetes service account token refresh [livy]
Posted by "gyogal (via GitHub)" <gi...@apache.org>.
gyogal commented on code in PR #536:
URL: https://github.com/apache/livy/pull/536#discussion_r3775663668
##########
server/src/main/scala/org/apache/livy/utils/SparkKubernetesApp.scala:
##########
@@ -77,26 +77,27 @@ object SparkKubernetesApp extends Logging {
}
val RefreshServiceAccountTokenThread = new Thread() {
+ // Token expires after 1 hour by default; refresh every 5 minutes.
+ private val tokenRefreshIntervalMs = 300000L
+
override def run(): Unit = {
- while (true) {
- var currentContext = new Context()
- var currentContextName = new String
- val config = kubernetesClient.getConfiguration
- if (config.getCurrentContext != null) {
- currentContext = config.getCurrentContext.getContext
- currentContextName = config.getCurrentContext.getName
+ while (!Thread.currentThread().isInterrupted) {
+ try {
+ val config = kubernetesClient.getConfiguration
+ val contextName = Option(config.getCurrentContext).map(_.getName).getOrElse("")
+ val newestConfig = Config.autoConfigure(contextName)
+ config.setOauthToken(newestConfig.getOauthToken)
+ info("Refreshed Kubernetes service account token.")
+ Thread.sleep(tokenRefreshIntervalMs)
+ } catch {
+ case e: InterruptedException =>
+ Thread.currentThread().interrupt()
+ warn("RefreshServiceAccountTokenThread was interrupted, exiting.", e)
+ return
+ case NonFatal(e) =>
+ warn(s"Failed to refresh Kubernetes service account token, will retry.", e)
+ Thread.sleep(tokenRefreshIntervalMs)
Review Comment:
@nileshrathi345 , would it be possible to move the `Thread.sleep()` out of the exception handler? What I have in mind is a scenario where `Thread.sleep()` is interrupted and the `InterruptedException` is not caught or handled somewhere else (not sure if this would cause any problems, but it looks a bit unpredictable where execution goes next).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@livy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [LIVY-1063] Fix resource leak in Kubernetes service account token refresh [livy]
Posted by "nileshrathi345 (via GitHub)" <gi...@apache.org>.
nileshrathi345 commented on code in PR #536:
URL: https://github.com/apache/livy/pull/536#discussion_r3776165560
##########
server/src/main/scala/org/apache/livy/utils/SparkKubernetesApp.scala:
##########
@@ -77,26 +77,27 @@ object SparkKubernetesApp extends Logging {
}
val RefreshServiceAccountTokenThread = new Thread() {
+ // Token expires after 1 hour by default; refresh every 5 minutes.
+ private val tokenRefreshIntervalMs = 300000L
+
override def run(): Unit = {
- while (true) {
- var currentContext = new Context()
- var currentContextName = new String
- val config = kubernetesClient.getConfiguration
- if (config.getCurrentContext != null) {
- currentContext = config.getCurrentContext.getContext
- currentContextName = config.getCurrentContext.getName
+ while (!Thread.currentThread().isInterrupted) {
+ try {
+ val config = kubernetesClient.getConfiguration
+ val contextName = Option(config.getCurrentContext).map(_.getName).getOrElse("")
+ val newestConfig = Config.autoConfigure(contextName)
+ config.setOauthToken(newestConfig.getOauthToken)
+ info("Refreshed Kubernetes service account token.")
+ Thread.sleep(tokenRefreshIntervalMs)
+ } catch {
+ case e: InterruptedException =>
+ Thread.currentThread().interrupt()
+ warn("RefreshServiceAccountTokenThread was interrupted, exiting.", e)
+ return
+ case NonFatal(e) =>
+ warn(s"Failed to refresh Kubernetes service account token, will retry.", e)
+ Thread.sleep(tokenRefreshIntervalMs)
Review Comment:
Good point — now I got it, having Thread.sleep() inside the NonFatal handler meant an interrupt during backoff wouldn't be caught cleanly.
Moved the sleep outside the exception handler so both success and failure paths wait the same way, and InterruptedException is handled in one place.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@livy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org