Skip to content

Update format.ml to avoid Queue.empty - #13572

Merged
gasche merged 19 commits into
ocaml:trunkfrom
craff:patch-5
Nov 20, 2024
Merged

gasche merged 19 commits into
ocaml:trunkfrom
craff:patch-5

Conversation

@craff

@craff craff commented Oct 25, 2024

Copy link
Copy Markdown
Contributor

issue #13463

@gasche

gasche commented Oct 26, 2024

Copy link
Copy Markdown
Member

I would find the code easier to explain if we did not print the token if we realize that the queue has become empty, something like:

match take_opt pp_queue with
| None -> ()
| Some ->
  (* printing logic *)

In any case, I think there should be a comment that explains the choice to deal with concurrent races in best effort.

@gasche

gasche commented Oct 26, 2024

Copy link
Copy Markdown
Member

Note: I am not, a priori, convinced that we should try to slightly better support this use-case, instead of failing loudly with an exception (as we were doing). Why would you try to rely on this, instead of fixing your code to avoid racing on format buffers?

@craff

craff commented Oct 28, 2024 via email

Copy link
Copy Markdown
Contributor Author

@craff

craff commented Oct 28, 2024

Copy link
Copy Markdown
Contributor Author

I would find the code easier to explain if we did not print the token if we realize that the queue has become empty, something like:

match take_opt pp_queue with
| None -> ()
| Some ->
  (* printing logic *)

In any case, I think there should be a comment that explains the choice to deal with concurrent races in best effort.

Done. Thanks for your comment.

@gasche

gasche commented Oct 28, 2024 •

Copy link
Copy Markdown
Member

Following the line of thoughts around "fail loudly if there is an issue": would failing with an Invalid_argument exception be okay, or are you saying that your code is catching all exceptions (including those that should not be recovered from) and that also would not work? If that would have worked for you, I think that invalid_arg "Format: unsychronized concurrent access" would have my preference over () in this case.

@dbuenzli

Copy link
Copy Markdown
Contributor

invalid_arg "Format: unsychronized concurrent access"

As was established in this PR there's no need for the "concurrent" but other than that I would much prefer if the stdlib fails loudly on race conditions rather than hiding them.

@craff

craff commented Oct 28, 2024 via email

Copy link
Copy Markdown
Contributor Author

@dbuenzli

dbuenzli commented Oct 28, 2024 •

Copy link
Copy Markdown
Contributor

Invalid_argument why not, but this is not really an invalid argument.

I think it's ok: you are giving an unsynchronized formatter to a formatting function. That's an invalid argument as per Format's expectations.

Assert could be good to pointing to the code in format?

I don't really care where the offending code is in Format. I only care to know that Format's expectations were violated and that I'm responsible for it, which is what Invalid_argument is for. If I see an Assert failure I assume the bug is about an assumption internal to the library which is not satisfied (i.e. that's something I need to report to the library), which is not the case here.

craff added 2 commits October 29, 2024 06:57
Use invalid_arg
Update documentation.
@craff

craff commented Oct 29, 2024

Copy link
Copy Markdown
Contributor Author

I updated the code to use invalid_arg and updated also the documentation to document this exception.

@craff

craff commented Oct 29, 2024

Copy link
Copy Markdown
Contributor Author

I am wondering if we could make it systematic that this exception is raised in case of thread unsafe use of the library?

@gasche

gasche commented Oct 29, 2024

Copy link
Copy Markdown
Member

My understanding is that invalid_arg is used to signal a programming error (a function was called without respecting some documented precondition) that is not intended to be caught by the caller. I already use Invalid_argument in this way in the Dynarray module (see documentation, the "Unsynchronized accesses" paragraph at the end of the toplevel comment), so in this sense it is arguably already a convention.

(Not completely fond of your error message but we are splitting hair. I'm sure @dbuenzli could make a good proposal and I would suggest to just reuse whatever he proposes.)

@dbuenzli

dbuenzli commented Oct 29, 2024 •

Copy link
Copy Markdown
Contributor

perhaps something like:

Format: Unsynchronized access to formatter 

or if the Format docs are good enough to allow the puzzled programmer to answer the question of what this could be:

Format: Unsynchronized access to formatter, use a synchronized formatter

@craff

craff commented Oct 30, 2024

Copy link
Copy Markdown
Contributor Author

perhaps something like:

Format: Unsynchronized access to formatter 

I just did that, and updated my modification mentionning the exception in Format.mli.

I still wounder if we can capture a few more case of unsynchronised use.

Note: when I do logging in simple_httpd, I use one distinct file and formatter per domain. No mutex this way and
the fist column being the unix epoch, it is easy to merge sort externaly.

@gasche gasche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine to me (see minor comments below). I'm not sure whether this qualifies as a "stdlib change that requires a second approval", but in case I'd ask @Octachron or @nojb or @OlivierNicole for a second opinion.

Comment thread Changes Resolved
Comment thread stdlib/format.ml Resolved
Comment thread stdlib/format.mli Resolved
@OlivierNicole OlivierNicole self-assigned this Oct 30, 2024

@Octachron Octachron left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raising a clear exception for certain unsynchronized access sounds like a good to me too.

Comment thread stdlib/format.mli Resolved
@gasche
gasche merged commit c839100 into ocaml:trunk Nov 20, 2024
@gasche

gasche commented Nov 20, 2024

Copy link
Copy Markdown
Member

Merged (by squashing all commits), thanks!

@craff

craff commented Nov 20, 2024

Copy link
Copy Markdown
Contributor Author

Thanks all for your comments!

Comment thread Changes

### Standard library:

- #13371: Avoid Queue.empty to be raised by Format when used concurrently.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incorrect number

@craff

craff commented Dec 2, 2024 via email

Copy link
Copy Markdown
Contributor Author

@Octachron

Copy link
Copy Markdown
Member

The convention is to cite all PRs and issues involved in the Changes entry (I will fix the Changes entry manually).

@craff

craff commented Dec 2, 2024

Copy link
Copy Markdown
Contributor Author

Gaëtan Gilbert @.***> writes:
@SkySkimmer commented on this pull request. ------------------------------------------------------------------------------------------------------------------------- In Changes: > @@ -67,6 +67,10 @@ Working version ### Standard library: +- #13371: Avoid Queue.empty to be raised by Format when used concurrently.
This is the number of the issue, not the number of the PR. I thought this was OK too ?
…
incorrect number — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.
-- Christophe Raffalli web: https://raffalli.eu

Argh, no you are right, this is another issue with format. As this is merged, I do not know really what to do ?

@Octachron

Copy link
Copy Markdown
Member

The typo are now fixed.

@craff

craff commented Dec 3, 2024 via email

Copy link
Copy Markdown
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants