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

Getting timeout while connecting to Github API #1926

Open
Mohazinkhan opened this issue Sep 10, 2024 · 13 comments
Open

Getting timeout while connecting to Github API #1926

Mohazinkhan opened this issue Sep 10, 2024 · 13 comments
Labels

Comments

@Mohazinkhan
Copy link

Mohazinkhan commented Sep 10, 2024

I have configured a Github App for Jenkins and I am running a Global seed job which would connect to the Github API and retrieve all the repositories in the Organization. Whenever it tries to authenticate to the Github API and retrieve the list of repositories it fails with timeout and the following error is displayed,

Caused: org.kohsuke.github.HttpException: Server returned HTTP response code: -1, message: 'null' for URL: [https://api.github.com/orgs/{orgname}]

Stacktrace: 

hudson.remoting.ProxyException: java.net.SocketTimeoutException: Connect timed out
	at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:551)
	at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:602)
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
	at java.base/java.net.Socket.connect(Socket.java:633)
	at java.base/sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:304)
	at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:178)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:533)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:638)
	at java.base/sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:266)
	at java.base/sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:380)
	at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:193)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1241)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1127)
	at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:179)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1686)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1610)
	at java.base/java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:529)
	at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:308)
	at org.kohsuke.github.GitHubHttpUrlConnectionClient.getResponseInfo(GitHubHttpUrlConnectionClient.java:69)
	at org.kohsuke.github.GitHubClient.sendRequest(GitHubClient.java:400)
Also:   hudson.remoting.ProxyException: org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: fa952780-e9a2-4351-b74d-d3851ac026e3
Caused: hudson.remoting.ProxyException: org.kohsuke.github.HttpException: Server returned HTTP response code: -1, message: 'null' for URL: https://api.github.com/orgs/
	at org.kohsuke.github.GitHubClient.interpretApiError(GitHubClient.java:500)
	at org.kohsuke.github.GitHubClient.sendRequest(GitHubClient.java:420)
	at org.kohsuke.github.GitHubClient.sendRequest(GitHubClient.java:363)
	at org.kohsuke.github.Requester.fetch(Requester.java:74)
	at org.kohsuke.github.GitHub.getOrganization(GitHub.java:505)
	at org.kohsuke.github.GitHub$getOrganization.call(Unknown Source)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
	at com.<orgname>.jenkins.jobdsl.GithubFetcher.<init>(GithubFetcher.groovy:19)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
	at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
	at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
	at org.codehaus.groovy.runtime.callsite.ConstructorSite.callConstructor(ConstructorSite.java:45)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:59)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:238)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:258)
	at uc_generator.generateUcRepos(uc_generator.groovy:38)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)
	at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
	at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:157)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:161)
	at uc_generator.run(uc_generator.groovy:8)
	at uc_generator$run.call(Unknown Source)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
	at uc_generator$run.call(Unknown Source)
	at PluginClassLoader for job-dsl//javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScript(AbstractDslScriptLoader.groovy:138)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)
	at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
	at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
	at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:64)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:169)
	at PluginClassLoader for job-dsl//javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:108)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
	at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:352)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1034)
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:68)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:177)
	at PluginClassLoader for job-dsl//javaposse.jobdsl.dsl.AbstractDslScriptLoader$_runScripts_closure1.doCall(AbstractDslScriptLoader.groovy:61)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
	at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:264)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1034)
	at groovy.lang.Closure.call(Closure.java:420)
	at groovy.lang.Closure.call(Closure.java:436)
	at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2125)
	at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2110)
	at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2163)
	at org.codehaus.groovy.runtime.dgm$165.invoke(Unknown Source)
	at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
	at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
	at PluginClassLoader for job-dsl//javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScripts(AbstractDslScriptLoader.groovy:46)
	at PluginClassLoader for job-dsl//javaposse.jobdsl.plugin.ExecuteDslScripts.perform(ExecuteDslScripts.java:363)
	at jenkins.tasks.SimpleBuildStep.perform(SimpleBuildStep.java:123)
	at PluginClassLoader for workflow-basic-steps//org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:101)
	at PluginClassLoader for workflow-basic-steps//org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:71)
	at PluginClassLoader for workflow-step-api//org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:840)
@bitwiseman
Copy link
Member

Jenkins GitHub plugin should be using okhttp3 everywhere, not HttpURLConnection. Are you using an old version of Jenkins or the GitHub plugin? Or perhaps running on Java 8?

@Mohazinkhan
Copy link
Author

Mohazinkhan commented Sep 10, 2024

@bitwiseman I am on Jenkins 2.468 and Github plugin 1.40.0 and on Java 7 (JDK 17.0.5) .. Is there any compatibility issue in this?

@Mohazinkhan
Copy link
Author

@bitwiseman I downgraded Github plugin version to 1.34. Still the same issue persists. Appreciate any advise on this.

@bitwiseman
Copy link
Member

bitwiseman commented Sep 11, 2024

@Mohazinkhan
JDK 7? Oh, 17. Okay.

What you're seeing is a continuation of #539 and #433 . The library has attempted multiple times to deal with this and things have improved. But the real solution is to not use HttpURLConnection. I'm not sure how it getting instantiated there, but it is.

In the upcoming v2.0 release, we'll be removing HttpURLConnection everywhere.

@Mohazinkhan
Copy link
Author

@bitwiseman Thanks for responding. Hmm, I tried every possible thing at my disposal to get rid of this issue, but I just could not. Apart from plugins github, github-api and github-branch-release, is there any other plugin that would be invoking HttpURLConnection?

Also, can you please give some expected timeline on when is this v2.0 release planned?

@Mohazinkhan
Copy link
Author

Mohazinkhan commented Sep 11, 2024

@bitwiseman Also, below is the piece of code I am executing to fetch the list of organization repositories using github API,
I am using GitHubBuilder method. Could that be the reason for HttpURLConnection??
https://github.com/hub4j/github-api/blob/main/src/main/java/org/kohsuke/github/GitHubBuilder.java#L15C17-L15C34

import javaposse.jobdsl.dsl.DslFactory
import org.kohsuke.github.GHFileNotFoundException
import org.kohsuke.github.GHOrganization
import org.kohsuke.github.GHRepository
import org.kohsuke.github.GitHub
import org.kohsuke.github.GitHubBuilder

class GithubFetcher {

    private final GHOrganization organization
    private final DslFactory dsl
    private static final MAIN_PIPELINE = ["": "Jenkinsfile"]

    private String githubOwner = 'orgname'
    
    GithubFetcher(String token, DslFactory dsl) {
        this.organization = getGithubConnection(token).getOrganization(githubOwner)
        this.dsl = dsl
    }
    
    private static GitHub getGithubConnection(String token) {
        return GitHubBuilder
                .newInstance()
                .withJwtToken(token)
                .build()
    }
    List<GHRepository> getAllRepos() {
        List<GHRepository> allRepos = organization
                .listRepositories(100)
                .findAll { !it.archived }
        dsl.println("All repos ${allRepos.size()}")
        return allRepos
    }

@bitwiseman
Copy link
Member

@Mohazinkhan
Oh, so you're running custom code inside Jenkins DSL. That explains it.

Change your code as follows:

import okhttp3.OkHttpClient;
import org.kohsuke.github.extras.okhttp3.OkHttpConnector;

// ...

private static GitHub getGithubConnection(String token) {
    return GitHubBuilder
            .newInstance()
            .withConnector(new OkHttpGitHubConnector(new OkHttpClient.Builder().build()))
            .withJwtToken(token)
            .build()
}

// ...

@Mohazinkhan
Copy link
Author

@bitwiseman oh yes, custom code in Jenkins DSL.
Thanks so much for the code update. I will try this and update you the results here :)

@Mohazinkhan
Copy link
Author

@bitwiseman I added the Gradle dependency for okhttp as follows,
implementation 'com.squareup.okhttp3:okhttp:4.12.0' and then modified the above lines of code as you provided.
Ended up with the java.lang.ClassNotFoundException: okhttp3.OkHttpClient error. Stacktrace below,
java.lang.ClassNotFoundException: okhttp3.OkHttpClient at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:593) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526) Also: org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: 37ca06ee-7e50-43cc-98ec-1a8450cd862a Caused: java.lang.NoClassDefFoundError: okhttp3/OkHttpClient at com.<org>.jenkins.jobdsl.GithubFetcher.getGithubConnection(GithubFetcher.groovy:29) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98) at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:151) at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.callStatic(StaticMetaMethodSite.java:102) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:55) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:197) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:209)

@bitwiseman
Copy link
Member

@Mohazinkhan
Here's a version with no external dependencies.

import org.kohsuke.github.extras.HttpClientGitHubConnector;

// ...

private static GitHub getGithubConnection(String token) {
    return GitHubBuilder
            .newInstance()
            .withConnector(new HttpClientGitHubConnector()))
            .withJwtToken(token)
            .build()
}

// ...

@Mohazinkhan
Copy link
Author

Hi @bitwiseman

This is the current error after updating with the recent code you provided.

java.nio.channels.ClosedChannelException at java.base/sun.nio.ch.SocketChannelImpl.ensureOpen(SocketChannelImpl.java:202) at java.base/sun.nio.ch.SocketChannelImpl.beginConnect(SocketChannelImpl.java:786) at java.base/sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:874) at java.net.http/jdk.internal.net.http.PlainHttpConnection.lambda$connectAsync$1(PlainHttpConnection.java:210) at java.base/java.security.AccessController.doPrivileged(AccessController.java:571) at java.net.http/jdk.internal.net.http.PlainHttpConnection.connectAsync(PlainHttpConnection.java:212) Caused: java.net.ConnectException at java.net.http/jdk.internal.net.http.common.Utils.toConnectException(Utils.java:1028) at java.net.http/jdk.internal.net.http.PlainHttpConnection.connectAsync(PlainHttpConnection.java:227) at java.net.http/jdk.internal.net.http.PlainHttpConnection.checkRetryConnect(PlainHttpConnection.java:280) at java.net.http/jdk.internal.net.http.PlainHttpConnection.lambda$connectAsync$2(PlainHttpConnection.java:238) at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:934) at java.base/java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:911) at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510) at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1773) Caused: java.net.ConnectException at java.net.http/jdk.internal.net.http.HttpClientImpl.send(HttpClientImpl.java:951) at java.net.http/jdk.internal.net.http.HttpClientFacade.send(HttpClientFacade.java:133) at org.kohsuke.github.extras.HttpClientGitHubConnector.send(HttpClientGitHubConnector.java:82) at org.kohsuke.github.GitHubClient.sendRequest(GitHubClient.java:461) Also: org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: 908a0208-d715-4911-ac93-c3222cd12c91

@Mohazinkhan
Copy link
Author

@bitwiseman
Also, I was getting the following error when using okhttp3.OkHttpClient.Builder,

java.lang.ClassNotFoundException: okhttp3.OkHttpClient$Builder at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:593) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526) Also: org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: db356d2b-85f4-45f7-9aac-98f62d622047 Caused: java.lang.NoClassDefFoundError: okhttp3/OkHttpClient$Builder at com.optiva.jenkins.jobdsl.GithubFetcher.getGithubConnection(GithubFetcher.groovy:33) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98) at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:151) at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.callStatic(StaticMetaMethodSite.java:102) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:55) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:197) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:209) at com.optiva.jenkins.jobdsl.GithubFetcher.<init>(GithubFetcher.groovy:24) at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486) at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83) at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77) at org.codehaus.groovy.runtime.callsite.ConstructorSite.callConstructor(ConstructorSite.java:45) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:59) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:238) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:258)

@bitwiseman
Copy link
Member

I'm sorry, I can't help you with that. Unexpected channel closing isn't a failure in this library. Maybe your network is having issues? When you figure out a solution please report back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants