Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue.add is not thread/signal safe #5309

Closed
vicuna opened this issue Jul 6, 2011 · 3 comments
Closed

Queue.add is not thread/signal safe #5309

vicuna opened this issue Jul 6, 2011 · 3 comments
Assignees
Labels

Comments

@vicuna
Copy link

@vicuna vicuna commented Jul 6, 2011

Original bug ID: 5309
Reporter: @lefessan
Assigned to: @lefessan
Status: closed (set by @xavierleroy on 2015-12-11T18:04:34Z)
Resolution: fixed
Priority: normal
Severity: minor
Platform: all
OS: all
OS Version: all
Version: 3.12.1+dev
Fixed in version: 3.13.0+dev
Category: ~DO NOT USE (was: OCaml general)
Monitored by: khooyp

Bug description

If an exception is raised in a signal after the first line of Queue.add (i.e. increasing the length counter of the queue), the queue invariants are broken and bad things can happen (segfault...).

In the additional information, I provide a new "add" function that should be thread/signal safe.

Additional information

let add x q =
if q.length = 0 then
let rec cell = {
content = x;
next = cell
} in
q.length <- q.length + 1;
q.tail <- cell
else
let tail = q.tail in
let head = tail.next in
let cell = {
content = x;
next = head
} in
q.length <- q.length + 1;
tail.next <- cell;
q.tail <- cell

@vicuna
Copy link
Author

@vicuna vicuna commented Jul 6, 2011

Comment author: @mmottl

Nitpicking here:

q.length <- q.length + 1;

can be replaced by:

q.length <- 1;

in the first branch.

@vicuna
Copy link
Author

@vicuna vicuna commented Feb 15, 2012

Comment author: @gasche

To sum up yesterday's discussion: the OCaml library is not guaranteed to be signal-safe, or rather, it is voluntarily guaranteed not to be signal-safe. Users should acknowledge that using mutable data structures expose them to signal hazards, and stick to immutable data structures or use different, specialized signal-hardened libraries.

That said, this specific patch is relatively harmless -- in particular, it does not impact clarity and readability -- and Fabrice may include it.

As a personal note: we may also write

begin if q.length = 1 then
..
else
..
end;
q.length <- q.length + 1

to avoid duplicating the common length-extension logic. Of course, it's only a dispensable detail.

@vicuna
Copy link
Author

@vicuna vicuna commented Feb 18, 2012

Comment author: @lefessan

Fixed in commit r12163 in SVN trunk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants