TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

We switched to Java 21 virtual threads and got a deadlock in TPC-C for Postgres

280 点作者 magden超过 1 年前

27 条评论

synthetigram超过 1 年前
This problem is not going to go away so easily. Numerous core Java classes (like BufferedInputStream) use synchronized. I count 1600+ usages in java.base. The blocking issue means it&#x27;s _much_ easier to accidentally run into this, rather than waving it away as an unlikely edge case.<p>I personally ran into this Using the built in com.sun webserver, with a virtual thread executor. My VPS only has two CPUs which means the FJP that virtual threads run on only have 2 active threads at a time. I ran into this hang when some of the connection hung, blocking any further requests from being processed.
评论 #39012631 未加载
评论 #39010868 未加载
评论 #39010647 未加载
评论 #39011328 未加载
mrintegrity超过 1 年前
Totally off topic but I am getting tired of the AI generated images used on nearly all blog posts nowadays. They are instantly recognisable, it just seems low effort and lowers the feeling of quality one might otherwise have
评论 #39009555 未加载
评论 #39009938 未加载
评论 #39011403 未加载
评论 #39009626 未加载
评论 #39009673 未加载
评论 #39010493 未加载
评论 #39012361 未加载
评论 #39010785 未加载
评论 #39009530 未加载
评论 #39010400 未加载
评论 #39012536 未加载
评论 #39010030 未加载
评论 #39009703 未加载
评论 #39012184 未加载
评论 #39010783 未加载
评论 #39010985 未加载
评论 #39011001 未加载
NovaX超过 1 年前
It is a known caveat that virtual threads do not work well with long running synchronization by pinning the thread. That unfortunately means that for many applications it may be premature to adopt them, but it is mature enough for broader evaluation by the libraries and frameworks. The Java team provided a status of their efforts recently [1].<p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=WoQJnnMIlFY&amp;t=421s" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=WoQJnnMIlFY&amp;t=421s</a>
评论 #39009726 未加载
评论 #39010648 未加载
评论 #39008521 未加载
papercrane超过 1 年前
Curious if you considered switching to a different connection pooling library. These days I usually use HikariCP which is fast an actively maintained. c3p0 hasn&#x27;t had any activity for years, I&#x27;m not sure if it&#x27;s still maintained.
评论 #39010614 未加载
评论 #39009961 未加载
评论 #39008829 未加载
评论 #39028192 未加载
评论 #39009610 未加载
vvern超过 1 年前
Go has a mechanism to spawn a new thread (m in ho runtime parlance) if it thinks one of its threads might be blocked in a cgo (go’s “native function” equivalent). That prevents stuff like this.
评论 #39009502 未加载
评论 #39009519 未加载
spintin超过 1 年前
The warning shots across the bow where heard with this statement from the devs:<p>&quot;Don&#x27;t replace platform&#x2F;native threads with virtual ones, replace tasks (without further explanation) instead&quot;?!<p>Combine that with the fact that they chose to implement the scheduler in Java instead of C(++) and you&#x27;re set for performance problems.<p>Remember that NIO took from 1.5 to 1.7 to be usable&#x2F;performant and that was native!<p>Edit: Finally figured out why: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=39010648">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=39010648</a>
评论 #39011236 未加载
评论 #39011102 未加载
评论 #39011117 未加载
saagarjha超过 1 年前
This is a common problem when migrating a system from threads to virtual threads. In general, using primitives which block the current thread and prevent forward progress can quickly lead to deadlocks. It’s a hard issue to catch because in the past usually this would get “solved” by spawning a new thread to complete the task but in a world with virtual threads the runtime is usually reluctant to spawn more threads, so there’s nothing that can service more work if you’ve blocked all the threads.
评论 #39008974 未加载
moonchild超过 1 年前
I am extremely confused.<p>&gt; There are two scenarios in which a virtual thread cannot be unmounted during blocking operations because it is pinned to its carrier:<p>&gt; When it executes code inside a synchronized block or method<p>Isn&#x27;t &#x27;synchronized&#x27; effectively sugar for taking a kind of lock? Why can&#x27;t it be treated uniformly by the scheduler?
评论 #39009606 未加载
评论 #39009357 未加载
taspeotis超过 1 年前
Did they get a deadlock again? <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=38939165">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=38939165</a>
评论 #39008336 未加载
dikei超过 1 年前
I wonder if HikariCP, currently the best Java DB Connection Pooling library, suffer the same issue as c3p0.
jshowalter超过 1 年前
Why not treat this as a bug, and fix it in Java 21? For compliance reasons we can only use LTS versions, and the next one isn&#x27;t until September 2025, according to <a href="https:&#x2F;&#x2F;www.oracle.com&#x2F;java&#x2F;technologies&#x2F;java-se-support-roadmap.html" rel="nofollow">https:&#x2F;&#x2F;www.oracle.com&#x2F;java&#x2F;technologies&#x2F;java-se-support-roa...</a>.
torrent超过 1 年前
Seems to be a similiar problem field as writing blocking functions that call async functions in C# and co-existence of synchronous and asynchronous code.<p>There are numerous recommendations such as<p><a href="https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;archive&#x2F;msdn-magazine&#x2F;2015&#x2F;july&#x2F;async-programming-brownfield-async-development#the-blocking-hack" rel="nofollow">https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;archive&#x2F;msdn-magazine&#x2F;2015...</a><p>Final phase is &quot;I hope these techniques will help you adopt async into your existing applications in a way that works best for you.&quot;
pierrebai超过 1 年前
I thought the blog was great but &quot;in summary&quot; conclusion bad.<p>The summary merely stated that Java virtual thread are great. I expected a summary of the problem and solution, for example something like:<p>When using Java 21 virtual threads, you can end-up starved of carrier threads due to all carrier threads waiting on a pool exhausted resources with no thread available to free such resources. The solution is to wrap those resources in a virtual-thread aware object. In our case, we solved our problem by wrapping connections in semaphores.
xyst超过 1 年前
Concurrency, parallelism. These are among the most misunderstood concepts in programming&#x2F;software development.<p>TLS (especially mutual TLS) and Oauth also join this club.
评论 #39008977 未加载
skyde超过 1 年前
Why synchronized block are not preemptible? When compiling<p>public void syncMethod() {<p><pre><code> synchronized(lock) { &#x2F;&#x2F; some code } </code></pre> }<p>they could translate to<p>public void syncMethod() {<p><pre><code> await reentrantLockAsync.lockAsync(); try { await somecodeAsync(); } finally { await lock.unlockAsync() } }</code></pre>
评论 #39022444 未加载
dxxvi超过 1 年前
Why was c3p0 used (its latest version was released in Dec 2019)? Those tests existed for a while and people were too lazy to replace c3p0 with something newer? I guess that they spent all their time to use virtual threads in those tests and had no time left to look at c3p0.
bheadmaster超过 1 年前
Why couldn&#x27;t JVM detect when all carrier threads are blocked, and just spawn more of them?
评论 #39011313 未加载
bob1029超过 1 年前
Sounds similar to the quirks you get with TPL in .NET under some circumstances. For library code, a ConfigureAwait(false) invoke should be considered to signify that the execution does not need to resume on the original thread.
nottorp超过 1 年前
Can someone explain what these virtual threads are in 2 sentences please?
评论 #39012702 未加载
评论 #39012608 未加载
DeathArrow超过 1 年前
Why dining philosophers from the image have more than two hands?
评论 #39010505 未加载
评论 #39018803 未加载
hoseja超过 1 年前
BTW, dining philosophers is an extremely clunky example.
评论 #39012884 未加载
oldgradstudent超过 1 年前
Java virtual threads did not cause a deadlock here.<p>The deadlock was a usage error.<p>A better title would be: Naively switching to Java virtual threads caused a deadlock in TPC-C for Progress SQL.
评论 #39009222 未加载
评论 #39008323 未加载
评论 #39008310 未加载
评论 #39008864 未加载
评论 #39008993 未加载
otterley超过 1 年前
&quot;Our PostgreSQL TPC-C implementation utilizes c3p0 for connection pooling...The problem is that...synchronized code might be deeply embedded within the libraries you use. In our case, it was within the c3p0 library. So, the fix is straightforward: we simply wrapped the connection with a java.util.concurrent.Semaphore. With this change, virtual threads are blocked on the semaphore and, crucially, release the carrier thread instead of delving inside c3p0. Thus, we never block inside c3p0 because we enter c3p0 code only when there is a free session available.&quot;
评论 #39008418 未加载
charleslmunger超过 1 年前
&quot;Why not to use java virtual threads&quot; -&gt; Fundamental features of the language treated as implementation details in libraries for more than two decades cause deadlock.
评论 #39009415 未加载
chucke1992超过 1 年前
Personally I am curious how these features like virtual threads are tested when developed.
评论 #39011107 未加载
schizofunky超过 1 年前
I think i have a more elegant solution for this deadlock:<p>&quot;Switch to haskell&quot;.
评论 #39009154 未加载
评论 #39011192 未加载
jake_morrison超过 1 年前
Erlang is based on virtual threads (confusingly called processes). The Erlang virtual machine schedules them on OS threads. Erlang processes communicate using message passing, preventing deadlocks. You can use millions of Erlang processes without problems, e.g., to handle millions of Elixir LiveView sessions.
评论 #39008902 未加载
评论 #39008645 未加载
评论 #39010127 未加载
评论 #39011143 未加载