Intermittent errors when pushing and cloning with JGit on Bitbucket 5.14+

git
bitbucket-server

#1

We use JGit for testing our hooks. It seems since Bitbucket 5.14.0 our tests intermittently fail when we perform clone or push operations. The error we sometimes get is:

org.eclipse.jgit.api.errors.TransportException: http://localhost:8080/bitbucket/scm/test/test.git: expected Content-Type application/x-git-receive-pack-result; received Content-Type application/x-git-receive-pack-advertisement
	at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:164)
	at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:80)
	at com.onresolve.bitbucket.groovy.test.BaseRepoTest.gitPush(BaseRepoTest.groovy:372)
	at com.onresolve.bitbucket.groovy.test.TestConditionFunctions.secure shell successes of #condition(TestConditionFunctions.groovy:538)
Caused by: org.eclipse.jgit.errors.TransportException: http://localhost:8080/bitbucket/scm/test/test.git: expected Content-Type application/x-git-receive-pack-result; received Content-Type application/x-git-receive-pack-advertisement
	at org.eclipse.jgit.transport.TransportHttp.wrongContentType(TransportHttp.java:588)
	at org.eclipse.jgit.transport.TransportHttp$Service.openResponse(TransportHttp.java:882)
(19 more lines...)

We’ve tried using the same version of JGit Bitbucket Server 5.14 uses for it’s testing, also we’ve upgraded to the most recent version of JGit but no luck.

We’re also using git version 2.7.4 which doesn’t support the new V2 protocol, but I wouldn’t have thought this would be an issue. We’d like to better understand this error and why it happens in 5.14+ and hopefully try to solve it.


#2

@adam.markham,

For starters, Bitbucket Server does not use JGit at runtime. Ever. And we strongly discourage plugin developers from doing so, because it is often a source of OutOfMemoryErrors since, unlike when we fork git, JGit’s memory usage is all in the Bitbucket Server process. We use it in a limited way in our integration tests for hosting (push/pull) just to verify that JGit works, because it follows different rules than git itself does.

In order for protocol v2 to come into play, you need Git 2.18+ installed on the server, and then 2.18+ on a client. JGit added preliminary support for protocol v2 in their 5.0 release, but we have not tested it yet. It’s possible that JGit’s protocol v2 support is incompatible with how Bitbucket Server handles protocol v2. You talk about JGit versions, but you never actually provide any specific ones. Care to do so?

Using a version of Git which doesn’t support protocol v2, like 2.7.4, means the server will “speak” protocol v1 to it automatically, even if the Git installed on the server is capable of speaking v2. We don’t “force” v2 just because the server’s Git version supports it; instead, it’s used by the server when the client explicitly requests it, which Git 2.17 and older clients are incapable of doing.

But Git and JGit are very separate things. The error you show is for a JGit push, but then you mention you tried using Git 2.7.4. Did that fail? You don’t say. If it did, what was the error message? Unlike JGit, Git doesn’t care about the “Content-Type” on the response, so if it did fail it definitely should have been with a different message.

Best regards,
Bryan Turner
Atlassian Bitbucket


#3

@bturner thanks for your reply. The version of JGit we’re using is 4.2.0.201601211800-r we’ve also tried with the latest version of JGit 5.1.2.201810061102-r but we get the same error. Sorry, I didn’t mean you were using JGit internally, it looks like for the integration tests you mention your using JGit 4.2.0.201601211800-r which is the same one we use.

We’re using Git version 2.7.4 to run our integration tests. Yes our tests sometimes fail with that, but it’s unexpected because as you said it doesn’t support V2 of the Git protocol and we’re just seeing the following error in Bitbucket 5.14+ on a push, but sometimes also on a clone using JGit.

org.eclipse.jgit.api.errors.TransportException: http://localhost:8080/bitbucket/scm/test/test.git: expected Content-Type application/x-git-receive-pack-result; received Content-Type application/x-git-receive-pack-advertisement
	at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:164)
	at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:80)
	at com.onresolve.bitbucket.groovy.test.BaseRepoTest.gitPush(BaseRepoTest.groovy:372)
	at com.onresolve.bitbucket.groovy.test.TestConditionFunctions.secure shell successes of #condition(TestConditionFunctions.groovy:538)
Caused by: org.eclipse.jgit.errors.TransportException: http://localhost:8080/bitbucket/scm/test/test.git: expected Content-Type application/x-git-receive-pack-result; received Content-Type application/x-git-receive-pack-advertisement
	at org.eclipse.jgit.transport.TransportHttp.wrongContentType(TransportHttp.java:588)
	at org.eclipse.jgit.transport.TransportHttp$Service.openResponse(TransportHttp.java:882)
(19 more lines...)

So far I’ve not been able to reliably reproduce this problem, it happens intermittently and for different tests which work fine with the same version of JGit and Git on Bitbucket Server versions lower than 5.14.

I’ve turned on debug logging in Bitbucket Server for this to see if it shows up anything, but it doesn’t appear to, we just see the following when the test is run, but I don’t see how it would contribute to this error if a gc was triggered:

2018-10-11 12:23:20 [AtlassianEvent::thread-2] DEBUG c.a.s.i.scm.git.gc.GitGarbageTruck - [TEST/test[203]] Scheduling garbage collection

Is there a way we can pull out the actual content that Bitbucket Server is returning to JGit? It seems that when JGit throws that exception it’s too late.

Thanks,
Adam