Skip to main content
Aotokitsuruya
Aotokitsuruya
Senior Software Developer
Published at

Kobako: Why Releasing the GVL Doesn't Always Let You Run More

This article is translated by AI, if have any corrections please let me know.
This article is part of Kobako series.

In From Default Allow to Deny — Balancing Convenience and Security, the security problems that come up when applying the Sandbox were mostly dealt with, and I think it’s good enough to use.

At this point, I started asking myself, “If the sandbox is fully isolated from Ruby, does it still need to be limited by the GVL?” The GVL (Global VM Lock) makes things safer to use, but it often keeps us from taking advantage of multi-core CPUs, and Kobako happens to be able to move work onto other cores.

Unlock

If we want to lift the restriction of the GVL, we first need to understand roughly how the GVL works, and why Kobako fits this scenario.

Suppose I want to read a file in Ruby. Ruby handles it like this:

  • Thread A schedules the “read” and then releases the GVL
  • Thread B starts working on other tasks
  • Thread A finishes reading and requests the GVL
  • Thread A gets the GVL and creates the Ruby object

Put in the simplest terms, modifying Ruby’s internal state at the same time can cause a Race Condition, so a single global lock makes everyone stop and wait for Thread A to create the object, which makes problems much less likely.

Moving to Kobako’s scenario, we’ll find that the “result” we want to read is really the same as the content of a file read, so it can be designed as:

  • Thread A schedules “execute script” and then releases the GVL
  • Thread B starts working on other tasks
  • Thread A sees Kobako return the result
  • Thread A gets the GVL and creates the Ruby object

The behavior is the same. The difference is that Kobako also allows binding objects, and then it behaves more like “streaming,” because Kobako will return a value at specific points in time, which Ruby needs to accept before continuing.

  • Thread A schedules “execute script” and then releases the GVL
  • Thread B starts working on other tasks
  • Thread A sees Kobako issue a method call (a return)
  • Thread A gets the GVL, handles the call, then releases the GVL
  • Thread B starts working on other tasks
  • Thread A sees Kobako return the result
  • Thread A gets the GVL and creates the Ruby object

There’s one or more extra points in the middle where the GVL has to be acquired, so the more often Kobako calls out from the Guest, the fiercer the competition for the GVL becomes.

These two scenarios happen to show two different characteristics, “compute-bound” and “I/O-bound,” and so the benefit of releasing the GVL lies more in letting Kobako make good use of CPU resources.

Challenge

Recently, to make Kobako more broadly applicable, or to start covering most non-software-development scenarios, it introduced a Virtual Filesystem (VFS) and Networking features, which once again raised the complexity of the problem.

For example, if we want Ruby to dynamically decide whether a path can be accessed, we might have an implementation like this.

1sandbox = 
2  Kobako::Sandbox.new(gvl: :hold)
3                 .mount("/data", Bind.new("/srv/shared")) do |path, _|
4                   path.start_with?("/data/")
5                 end
6
7Thread.new { sandbox.eval('File.write("/data/a.txt", "A")') }   # Invocation A
8Thread.new { sandbox.eval('File.write("/data/b.txt", "B")') }   # Invocation B

In this state, the following situation can occur:

  • The Thread A runs on holds the GVL
  • B wants to write, so it takes the lock on the /data directory and prepares to request the GVL so it can call the Block to check permissions
  • A also wants to write, so it prepares to take the lock on the /data directory
  • Both A and B need the GVL and the lock on /data at the same time to proceed, but neither can get both

This is a classic Deadlock, and it happens only because we lock to keep file operations in the sandbox safe, which then conflicts with the GVL and gets stuck.

If Kobako didn’t provide the Policy mechanism (checking dynamically in Ruby) at all, the problem wouldn’t happen, but if you implement Kobako’s filesystem in Ruby, you’d still run into it

The fix isn’t complicated either. We just need to add a new rule, “when you have to wait for a directory lock, release the GVL first,” and the process becomes:

  • The Thread A runs on holds the GVL
  • B wants to write, so it takes the lock on the /data directory and prepares to request the GVL so it can call the Block to check permissions
  • A also wants to write, releases the GVL, then requests the lock on the /data directory
  • The Thread B runs on gets the GVL, finishes its work, and releases the /data lock
  • A gets the lock on the /data directory, takes back the GVL, and finishes its work

Locks like this increased quite a bit after the virtual filesystem and networking features were added, and maintenance became much more complicated. That’s why I brought in formal verification (TLA+): first define the main locking patterns, use TLA+ to exhaustively find where things can deadlock, and then implement according to those principles.

Kobako actually doesn’t know the GVL exists. Kobako’s core just sends a “this will wait” message, and on the Ruby side that “wait” is implemented by releasing the GVL. So even if we switch to a language like Python that has a mechanism similar to Ruby’s, it can still connect smoothly

Trade-off

After these adjustments, coming back to the limitation of the GVL itself, “running more” isn’t necessarily the ideal outcome, because of differences in workload type.

Looking at Kobako’s original positioning as an Agent Sandbox or Code Mode, the workload is closer to “I/O-bound” than “compute-bound.” With the Bind service mechanism described earlier, when the Guest calls the Host it’s like streaming piece by piece, and each call is an interruption point similar to I/O handling. Spreading more sandboxes across other cores doesn’t help much.

If we switch to a GVL-free version, the Threads requesting the GVL basically go from 1 (we usually don’t specifically use Thread.new) to N, and in the current Kobako these Threads are native threads not managed by Ruby, so we end up with many sandboxes asking “Can I use the GVL?” at the same time.

It becomes a kind of “queueing” problem. With the original GVL, everyone lines up and moves forward in order, but after lifting the GVL restriction, everyone jumps into the line at the same time and has to line up again after finishing each thing. When more people are in line, it actually gets slower.

So for Kobako, the upper limit of sandboxes is roughly the number of operations the machine can handle per second under the GVL. Even if more CPU cores could be used, it won’t get faster. The number of sandboxes can keep going up, but the rate at which they’re consumed has a limit.

In actual testing, once the number of concurrently running Agents exceeds what the GVL can handle, gvl: :hold just makes everyone queue and wait, while the GVL-released version’s throughput actually drops to less than half, and adding more makes it crash outright

That said, this is the design for the Agent Sandbox scenario. If we wanted to become a service like Cloudflare Workers, it would likely be “compute-bound,” letting every CPU process requests as much as possible. In that case, under the same latency target (for example, 100ms), we could get several times the throughput of using the GVL.

So running more isn’t necessarily a good metric. We still need to design according to the workload, and for Kobako, choosing a design that works well in most scenarios will be the biggest challenge.