Home

Nonreentrant

Nonreentrant describes a function or routine that cannot safely be entered again before the first invocation has completed. In practice, if a nonreentrant function is interrupted (by a signal, an interrupt handler, or another thread) and called again, it may corrupt data, return incorrect results, or crash. Reentrancy is closely related to, but not identical to, thread safety; a reentrant function can be safely interrupted and re-entered, whereas a nonreentrant one is not.

Several common causes of nonreentrancy include the use of static or global state that is modified during

Common examples include certain string-tokenization routines, memory allocators, or I/O wrappers that hold internal state. In

Mitigations focus on designing functions to be stateless or to operate on per-call context. Approaches include

execution,
reliance
on
non-thread-safe
library
calls,
and
the
use
of
static
buffers
or
stateful
resources.
Functions
that
maintain
internal
pointers,
buffers,
or
counters
without
ensuring
isolation
between
invocations
are
typical
examples.
Interacting
with
shared
I/O
devices
or
memory
without
proper
synchronization
can
also
create
nonreentrancy.
C,
for
instance,
strtok
is
nonreentrant
because
it
uses
a
static
pointer
to
track
its
position;
strtok_r
provides
a
reentrant
alternative.
Nonreentrancy
is
a
concern
in
both
multithreaded
programs
and
systems
with
interrupt
handling,
where
a
running
function
could
be
interrupted
and
subsequently
invoked
again.
removing
internal
static
data,
passing
explicit
context
objects,
allocating
memory
from
the
caller,
using
reentrant
or
thread-safe
library
variants,
and
employing
appropriate
synchronization
when
shared
resources
must
be
accessed
concurrently.