---
title: "How .NET differs on Isoplane"
canonical: https://isoplane.dev/docs/concepts/dotnet-runtime-differences
---

# How .NET differs on Isoplane

An Isoplane application is ordinary .NET code that the Isoplane engine runs
in a WebAssembly sandbox rather than as a process of its own. This page
covers the runtime behaviour that differs from the same code running as an
ordinary .NET process, what each difference means for your code, and what to
do about it. Behaviour the page does not mention is unchanged. If your
application already awaits its I/O, expect a few changes, mostly to state
kept in memory, to code that waits synchronously, and to background work.

Three facts about the sandbox explain the differences:

1. **The application runs as ephemeral instances.** Every instance is a
   [copy-on-write](https://en.wikipedia.org/wiki/Copy-on-write) clone of the
   *template*, which is the application's memory saved once its startup code
   has run.
2. **Isoplane enforces [cooperative
   concurrency](https://en.wikipedia.org/wiki/Cooperative_multitasking).**
   An instance's code runs on at most one thread at a time, and occupies that
   thread until it suspends on an `await`. Only then can the thread run other
   code, whether in the same instance or in another instance. The engine
   [stops a request that runs for too long](#running-time) and
   [prevents blocking operations](#blocking-waits), so instances do not rely
   on one another to yield.
3. **All I/O is
   [mediated](https://en.wikipedia.org/wiki/Capability-based_security) by the
   Isoplane engine.** The engine acts as a [reference
   monitor](https://en.wikipedia.org/wiki/Reference_monitor) for the instance.
   Your code uses the ordinary .NET APIs; underneath, every network, file,
   clock and operating-system call is a call into the engine, which performs
   it, accounts for it, and can refuse it.

## Instances and the template

### Every instance starts from a template

When an application is deployed, the engine runs its startup code and its
initialization callbacks, together called *template initialization*, and then
saves the application's memory, all before serving any request. That saved
memory is the *template*, and every instance starts as a copy of it. A fresh
.NET process would spend its first seconds on startup work and on the
one-time work its first requests trigger. A new instance skips all of that,
because it begins where template initialization left off. Expensive setup,
such as building caches or warming up code paths, therefore belongs in
template initialization, not in the first request. For what initialization
may and may not do, see
[the application lifecycle](https://isoplane.dev/docs/concepts/application-lifecycle#template-initialization).

Every instance starts with identical memory, down to any random values
initialization generated. Changing a setting makes the engine run
initialization again and save a new template. Initialization may also run
more than once before a template is saved, so initialization callbacks need
to be safe to repeat.

Initialization must also close every connection and stream it opens, because
an open connection cannot be saved in the template. The engine refuses to
save a template while a connection is open. During preparation, .NET makes a full
handshake for each new outbound TLS connection. Once an instance starts, it can
resume TLS sessions established by that instance.

Because the template is identical in every instance, anything that has to
differ between instances, such as an instance identifier or a nonce, has to
be written in an *instance start* callback, which runs in each instance
before its first request. A value initialization already generated can be
overwritten there. The engine reseeds the framework's random number
generators at the same point. For which generators, and an example callback,
see [instance start](https://isoplane.dev/docs/concepts/application-lifecycle#instance-start).

### Instances come and go

The engine adds instances as traffic grows and discards them when they are no
longer needed. When it discards an instance, it normally waits until no
request is running on it. The exceptions are an instance that [ignores
cancellation](#running-time) or overruns its memory limit, which the engine
stops while requests are still running. For the memory limit, see
[Resource limits](https://isoplane.dev/docs/concepts/resource-limits#memory).

Successive requests from one client can reach different instances, and
nothing ties a client to an instance. If your application already runs as
several processes behind a load balancer, this is familiar; the difference is
that the engine decides how many instances exist and when each instance starts
and stops.

### Disposal and finalizers

`finally` blocks, `using` and `await using` run as expected, except when an
instance is discarded while a dispatch is still running. Finalizers may not
run at all, because a finalizer runs only after a garbage collection and an
instance can be discarded before a collection happens. When the engine
discards an instance, nothing in it runs again: no `finally` block, no
`Dispose`, no finalizer. Dispose what you open before the response is sent,
and do not rely on a finalizer.

## Cooperative concurrency

### Dispatches and slices

A *dispatch* is one unit of work the engine gives an instance. The engine
starts a dispatch when a request arrives, and when background work such as a
timer callback is due. A *slice* is one uninterrupted run of a dispatch's
code, from when it starts or resumes until it awaits something or finishes.
No two slices run at the same time within an instance.

Dispatch 1

Dispatch 2

Dispatch 3

0 ms

50

100

150

200

250

300

350

Each row is one dispatch, started by a request. The pale band runs from the
request's arrival to its response. Within the band, a solid bar is a slice, a
dashed line is a wait on an operation, and a hollow bar is time spent ready
while another slice is running. A dispatch becomes ready when the operation it
awaited completes, and runs when the thread is free, so slices run in whatever
order dispatches become ready. Where no row has a solid bar, the instance
occupies no thread.

The engine runs slices on a pool of *execution threads* and performs the
instances' I/O on a separate pool of *reactor threads*. When an operation
completes on a reactor thread, the engine adds the continuation it resolved to
the execution pool's queue. The next slice begins when a free execution thread
pops that continuation from the queue.

The new slice belongs to the dispatch that owned the operation if that
dispatch is still active; if the dispatch has finished, the slice runs as a
new dispatch of [background work](#background-work), which counts against the
instance's [background allowance](https://isoplane.dev/docs/concepts/resource-limits#background-allowance).

An instance occupies an execution thread only while a slice is running, which
is how the engine serves far more instances than it has threads. If you know
Node.js, this is the shape of V8 and libuv, with application code on one
thread per instance, I/O in parallel on a pool, and completions queued back to
the application thread.

### One thread per instance

The sandbox excludes WebAssembly's shared memory and atomics, so only one
thread can ever execute inside an instance. `new Thread(...).Start()`
therefore throws `PlatformNotSupportedException`, and
`Environment.ProcessorCount` is 1.

`Task.Run`, `ThreadPool.QueueUserWorkItem` and `Parallel.For` do not throw.
They run on the instance's one thread, so queued work runs the next time your
code awaits, and parallel loops run their iterations one after another. They
lose their speed-up, but they do not need removing.

Even though an instance's own code executes serially, the engine runs multiple
I/O operations from the same instance in parallel on the reactor thread pool.
For the best I/O throughput, start several asynchronous operations without
awaiting them, then await them together with `Task.WhenAll` or `Task.WhenAny`.
To keep a long computation from blocking the instance, see [Running time and
cancellation](#running-time).

### Blocking waits and synchronous I/O

Isoplane is designed to support extremely dense, oversubscribed deployments,
while offering response latencies that are as good as, if not better than, all
other current .NET hosting options that offer scale-to-zero. The initial
engineering goal is to support 1,000 instances running typical .NET API
workloads on a single 16-core host machine with 64 GiB of system memory.

This goal hinges on a simple rule: no instance may perform any synchronous
blocking I/O or wait operations. A blocked instance would occupy an execution
thread without doing any work, and the host machine has far fewer execution
threads than instances. The .NET runtime enforces this rule by throwing
`PlatformNotSupportedException` any time a call would result in a synchronous
blocking wait, including calls made inside an `async` method.

This means that your code must call the asynchronous form of any operation that
would ordinarily block, and must `await` the result wherever it needs a wait or
an I/O operation to finish before it continues.

It's important to note that the runtime only throws in cases where proceeding
with execution would *actually* result in a blocking wait. If a method or
operation only sometimes results in a blocking wait, the runtime won't throw in
the cases when no blocking wait would occur. For example, the runtime won't
throw on calls to a synchronous method that accepts a timeout argument if the
timeout is set to `0`, provided that the implementation of that method returns
immediately rather than blocking when a zero timeout is specified.

The Roslyn analyzer that ships with Isoplane reports potentially blocking waits
as [`ISOPLANE1003`](https://isoplane.dev/docs/reference/error-codes#isoplane1003). See [Find and
fix blocking calls](https://isoplane.dev/docs/how-to/find-and-fix-blocking-calls) for an example.

See the table below for a list of common runtime operations that may trigger a
`PlatformNotSupportedException` due to this rule, and how to keep that from
happening:

| Call                                                                                            | Throws                                                       | Use instead                                                                             |
| ----------------------------------------------------------------------------------------------- | ------------------------------------------------------------ | --------------------------------------------------------------------------------------- |
| `task.Wait()`, `task.Result`                                                                    | when the task has not finished                               | `await task`                                                                            |
| `Task.WaitAll(...)`                                                                             | when a task has not finished                                 | `await Task.WhenAll(...)`                                                               |
| `Task.WaitAny(...)`                                                                             | when no task has finished                                    | `await Task.WhenAny(...)`                                                               |
| `semaphore.Wait()` on a `SemaphoreSlim`                                                         | when no slot is free                                         | `await semaphore.WaitAsync()`                                                           |
| `Wait()` on a `ManualResetEventSlim` or a `CountdownEvent`                                      | when the event is not set, or the count has not reached zero | `await` a `TaskCompletionSource`                                                        |
| `WaitOne()`, `WaitHandle.WaitAny(...)` and `WaitHandle.WaitAll(...)` on events or a `Semaphore` | when the handles are not signalled                           | `await` a `TaskCompletionSource`, or `await semaphore.WaitAsync()` on a `SemaphoreSlim` |
| `Take()` and `Add()` on a `BlockingCollection`                                                  | when it is empty, or full                                    | `await` `ReadAsync` and `WriteAsync` on a `Channel`                                     |
| `barrier.SignalAndWait()`                                                                       | unless this is the last participant to arrive                | no replacement; remove the call                                                         |
| `SpinWait.SpinUntil(...)`                                                                       | when the condition is false                                  | `await` the work the condition waits for                                                |
| `Monitor.Wait(...)`                                                                             | always, except with a timeout of zero                        | `await` a `TaskCompletionSource`                                                        |
| `Thread.Sleep(...)`                                                                             | always, except `Thread.Sleep(0)`                             | `await Task.Delay(...)`                                                                 |
| `Thread.CurrentThread.Join(...)`                                                                | always, except with a timeout of zero                        | no replacement; remove the call                                                         |

### Locks

A `lock` block cannot contain an `await`, so it always runs inside a single
slice. No other code runs during a slice, so the lock is never contended and
is always acquired at once. The same is true of `Monitor`, `Mutex`,
`ReaderWriterLockSlim` and `System.Threading.Lock` when they are used the
same way, around code that does not await, and `Lazy<T>` and the
`Concurrent*` collections work unchanged for the same reason.

A thread-owned lock held across an `await` protects nothing. A lock acquired
with `Monitor.Enter` and released after an `await` is owned by the thread, and
there is only one thread, so the next dispatch to reach the lock arrives on
the owning thread. A reentrant lock lets it in silently, and two dispatches
run inside the lock at once with nothing to report it. A non-reentrant lock
throws `LockRecursionException`.

A `SpinLock` created with `enableThreadOwnerTracking` off cannot tell that it
is already held, so re-entering it would spin forever; it throws
`PlatformNotSupportedException` instead.

For mutual exclusion across an `await`, use a `SemaphoreSlim` with
`await WaitAsync()`, and give the wait a timeout and the request's
cancellation token.

### Running time and cancellation

The engine meters each dispatch by its *running time*, the total duration of
its slices plus the time reactor threads spend processing its I/O. Running
time plays the role that CPU time plays for a process. It counts the time the
dispatch's code is executing, and not the time the dispatch spends waiting
between slices. Each request has a [running-time
allowance](https://isoplane.dev/docs/concepts/resource-limits#running-time), and background work has a
[separate allowance](https://isoplane.dev/docs/concepts/resource-limits#background-allowance).

Every request carries a cancellation token,
[`RequestHead.Aborted`](https://isoplane.dev/docs/reference/sdk/Isoplane.Sdk/Isoplane/RequestHead/Aborted)
in the SDK, which a web framework exposes through its own request
abstraction. The engine cancels the token when the request's allowance runs
out and when the client disconnects, and the request then has a grace period
in which to finish.

Honour cancellation promptly

If a cancelled request is still running when its grace period ends, the
engine [discards the whole instance](#ephemeral-instances), and every other
request on it fails too. Pass the token to everything you await, and let
`OperationCanceledException` end the request. For the allowance and the
grace period, see [Resource limits](https://isoplane.dev/docs/concepts/resource-limits#running-time).

Cancellation reaches your code only at an `await`. While a method computes
without awaiting, nothing else on the instance runs: not the token's
callbacks, and not the other requests. Split a long computation into parts,
and `await Task.Yield()` between them. A yield ends the slice, which gives the
instance's other requests a turn and lets a cancellation reach the token.
Check the token after each yield, for example with
`cancellationToken.ThrowIfCancellationRequested()`.

### Background work

*Background work* is any asynchronous task that runs on an instance after the
request that scheduled it has completed, or that no request scheduled, such as
a timer left running by template initialization.

The engine runs background work within the instance's
[background allowance](https://isoplane.dev/docs/concepts/resource-limits#background-allowance), and only
while the instance exists.

Background work is not guaranteed to run

It may run late, or not at all. Finish anything the response depends on
before sending the response.

For work that must happen, use a durable queue outside the instance. For
work that must run continuously or on a schedule, expose an endpoint and
have a scheduler call it; see
[Run work on a schedule](https://isoplane.dev/docs/how-to/run-work-on-a-schedule).

## All I/O goes through the engine

Your application has no direct access to the network, the file system, the
clock or the operating system. The engine provides its own version of all
four, and the sections below describe how those versions differ from what an
ordinary process gets.

### Incoming requests

The engine receives HTTP requests and routes each request to an instance with
available capacity. The application runs no web server of its own and cannot
listen on a port. The engine serves plain HTTP, and TLS is terminated in front
of it; see [Deploy to your own host](https://isoplane.dev/docs/how-to/deploy-an-application).

Requests arrive as HTTP/1.1, without connection upgrades or trailers, so
WebSockets are unavailable. Streamed responses work.

### Outgoing connections

An application can open TCP connections, with or without TLS, through the
asynchronous APIs: `HttpClient`, sockets and database drivers all work.
There is no UDP, so where a library offers a choice of transport, choose TCP
or HTTP.

Each instance has its own
[outbound connection limit](https://isoplane.dev/docs/concepts/resource-limits#outbound-connections), and
each instance pools connections on its own. A pool size in your
configuration therefore caps one instance, not the application. The engine
never closes an idle connection on the application's behalf, so with many
instances the total can exceed what your database allows. Cap connections
with an external pooler, in front of the database. The
[Connect to PostgreSQL](https://isoplane.dev/docs/how-to/connect-postgresql) guide turns the
driver's own pool off for this reason.

The engine clamps socket buffer sizes and keep-alive settings to fixed
ranges, and reading an option back returns the value in force; for the
ranges, see [Resource limits](https://isoplane.dev/docs/concepts/resource-limits#socket-options).
`Socket.Ttl` throws `SocketException` with `OperationNotSupported`.

### The clock

The engine reads the clock when a slice starts and gives the instance that
one value for the whole slice. Every reading within a slice, from
`DateTime.UtcNow`, `Stopwatch` or `Environment.TickCount64`, returns the
same millisecond. Timers and cancellation tokens have the same resolution
and follow the same rule, so a timer that is due during a slice fires in the
next slice.

The clock is frozen deliberately. A fine-grained clock is what timing
side-channel attacks need, and the engine withholds such a clock from the
code it runs, including dependencies you did not write.

What this means for your code:

- `Stopwatch` cannot time a computation. Within a slice it always reads zero
  elapsed.
- Across an `await` that suspends, `Stopwatch` measures from the start of one
  slice to the start of the next, so it reports an upper bound on the wait.
- Timestamps taken in one slice are identical. Log lines written together
  share a time.
- A loop that polls its own deadline without awaiting never sees the deadline
  arrive. Split the work and await between the parts.
- `Task.Delay`, `CancelAfter` and `Timer` callbacks fire between slices, never
  in the middle of a computation.

### File system

The file system is read-only and holds only the application package: the
assemblies, settings files and static content the build produced. Writes fail,
and there is nowhere to put a temporary file. To work without a temporary
file, see [Handle uploads and generated
files](https://isoplane.dev/docs/how-to/handle-uploads-and-generated-files).

A file's modification time is the time it was last written before the
application was built. To make it identical from build to build, see [Build
the same package on any
machine](https://isoplane.dev/docs/how-to/run-and-rebuild#build-the-same-package-on-any-machine).

### Environment variables and processes

Settings reach the application through the SDK's
`RuntimeConfiguration.ReadValues()`, which a framework's configuration system
reads from, never through the environment. For where they come from while you
develop, see [Local development settings](https://isoplane.dev/docs/how-to/local-settings).

There are no other processes, and an application cannot start a process.
Replace a child process with a managed library, or with a call to a service
that does the work.

### Native libraries

The .NET runtime Isoplane ships includes .NET's own native components for
compression, globalization and cryptography, as well as
[SQLite](https://isoplane.dev/docs/concepts/library-compatibility#sqlite), all compiled to WebAssembly. Your
application package ships managed code only. A native binary in a NuGet
package is never loaded, and a P/Invoke into such a binary fails when it is
called, not at build or startup. A package that ships native code still works
as long as the code paths you use never call it.

The analyzer can report native-import declarations as
[`ISOPLANE1001`](https://isoplane.dev/docs/reference/error-codes#isoplane1001). To switch that on,
see [Control compatibility
warnings](https://isoplane.dev/docs/how-to/run-and-rebuild#control-compatibility-warnings).

### Culture and time zone

Local time is UTC and the default culture is invariant, because an instance
is not tied to a machine with a locale of its own. Named cultures and time
zones are all available, and
[.NET's globalization guidance](https://learn.microsoft.com/dotnet/core/extensions/globalization)
applies unchanged.

## Where to go next

- Bringing an existing application? Work through the checklist in
  [Bring an existing ASP.NET application](https://isoplane.dev/docs/how-to/onboard-an-application).
- Starting fresh? The [project templates](https://isoplane.dev/docs/reference/project-templates)
  already follow everything on this page.
- Using a package that does one of the things above? See
  [Library compatibility](https://isoplane.dev/docs/concepts/library-compatibility) for which packages fail
  and what to use instead.
- Looking for the numbers? See [Resource limits](https://isoplane.dev/docs/concepts/resource-limits).
