This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Combinator Parsers trait is not thread safe ?!

5 replies
a.efremov
Joined: 2009-09-27,
User offline. Last seen 2 years 26 weeks ago.

Hello All,

as far as I understand scala is functional / immutable language.

Taking look at Parsers:131, lastNoSuccess catches my eyes. then I see
declaration var lastNoSuccess: ... at line 125. Then how It can be
thread safe?

I have built a singleton parser extending RegexParser and having very
intensive calculation. Thread pool have not time to sleep. very quickly
I got this nasty NP:
java.lang.NullPointerException
at scala.util.parsing.combinator.Parsers
$NoSuccess.(Parsers.scala:131)
at scala.util.parsing.combinator.Parsers
$Failure.(Parsers.scala:158)
at scala.util.parsing.combinator.RegexParsers$$anon
$2.apply(RegexParsers.scala:64)
at scala.util.parsing.combinator.Parsers$Parser$$anonfun$append
$1$$anonfun$apply$1.apply(Parsers.scala:208)
at scala.util.parsing.combinator.Parsers$Parser$$anonfun$append
$1$$anonfun$apply$1.apply(Parsers.scala:208)
at scala.util.parsing.combinator.Parsers
$Failure.append(Parsers.scala:162)
at scala.util.parsing.combinator.Parsers$Parser$$anonfun$append
$1.apply(Parsers.scala:208)
at scala.util.parsing.combinator.Parsers$Parser$$anonfun$append
$1.apply(Parsers.scala:208)
at scala.util.parsing.combinator.Parsers$$anon
$3.apply(Parsers.scala:182)
at scala.util.parsing.combinator.Parsers$Parser$$anonfun$append
$1.apply(Parsers.scala:208)
at scala.util.parsing.combinator.Parsers$Parser$$anonfun$append
$1.apply(Parsers.scala:208)
at scala.util.parsing.combinator.Parsers$$anon
$3.apply(Parsers.scala:182)

full stack trace gets up to 130 lines. I skip these useless details.

Definitely I misunderstand something. Nothing tells me about what I can
use with actors and what I don't.

alexander efremov

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Combinator Parsers trait is not thread safe ?!

On Thu, Dec 09, 2010 at 02:30:40AM +0100, Alexander Efremov wrote:
> as far as I understand scala is functional / immutable language.

That's only true if we don't go beyond handwaving. It would be more
accurate to say that scala gently hints immutability would be great, but
if you're not into it scala will still consort with you.

> Taking look at Parsers:131, lastNoSuccess catches my eyes. then I see
> declaration var lastNoSuccess: ... at line 125. Then how It can be
> thread safe?

It isn't.

http://scala-programming-language.1934581.n4.nabble.com/Scala-Parsers-ar...

> Definitely I misunderstand something.

I don't think you misunderstand anything, but your expectations might be
too high. There's no mechanism in the language to keep someone from
introducing mutable state, and all the software in trunk was written by
fallible and not terribly numerous humans.

Aydjen
Joined: 2009-08-21,
User offline. Last seen 1 year 28 weeks ago.
Re: Combinator Parsers trait is not thread safe ?!

Alexander Efremov wrote:
> Nothing tells me about what I can
> use with actors and what I don't.

I feel with you. My hope is that scala in some not too distant future
gains some sort of effects system. Right now all (?) there can be done
is to document thread-safety on a case-by-case,
best-effort-but-nothing-guaranteed basis.

Brian Goetz et al. are suggesting documenting a class' thread safety
with annotations (@Immutable and @ThreadSafe or @NotThreadSafe) in their
book "Java concurrency in practice". Maybe it would be worth considering
doing this for the classes in the scala library (in the absence of an
effect system).

Kind regards
Andreas

Razvan Cojocaru 3
Joined: 2010-07-28,
User offline. Last seen 42 years 45 weeks ago.
RE: Combinator Parsers trait is not thread safe ?!

>Brian Goetz et al. are suggesting documenting a class' thread safety with annotations (@Immutable and @ThreadSafe or >@NotThreadSafe) in their book "Java concurrency in practice". Maybe it would be worth considering doing this for the classes in >the scala library (in the absence of an effect system).

+1

-----Original Message-----
From: Andreas Flierl [mailto:andreas [at] flierl [dot] eu]
Sent: December-09-10 1:45 AM
To: Alexander Efremov
Cc: scala-user [at] listes [dot] epfl [dot] ch
Subject: Re: [scala-user] Combinator Parsers trait is not thread safe ?!

Alexander Efremov wrote:
> Nothing tells me about what I can
> use with actors and what I don't.

I feel with you. My hope is that scala in some not too distant future gains some sort of effects system. Right now all (?) there can be done is to document thread-safety on a case-by-case, best-effort-but-nothing-guaranteed basis.

Brian Goetz et al. are suggesting documenting a class' thread safety with annotations (@Immutable and @ThreadSafe or @NotThreadSafe) in their book "Java concurrency in practice". Maybe it would be worth considering doing this for the classes in the scala library (in the absence of an effect system).

Kind regards
Andreas

Philipp Dörfler
Joined: 2010-05-24,
User offline. Last seen 42 years 45 weeks ago.
Re: Combinator Parsers trait is not thread safe ?!
2010/12/9 Andreas Flierl <andreas [at] flierl [dot] eu>
Brian Goetz et al. are suggesting documenting a class' thread safety with annotations (@Immutable and @ThreadSafe or @NotThreadSafe) in their book "Java concurrency in practice". Maybe it would be worth considering doing this for the classes in the scala library (in the absence of an effect system).

+1 by me, too. Those annotations have proved to be useful in the past already.
Sadek Drobi
Joined: 2010-09-22,
User offline. Last seen 42 years 45 weeks ago.
Re: Combinator Parsers trait is not thread safe ?!
Actually I am afraid ThreadSafe can mean different things to different people. Things can be synchronized but can yet cause race conditions and dead locks. I guess what matters is referential transparency. So an annotation of ReferentiallyTransperent can mean that the interface is intended to be though and any observable effect is rather a bug. An example is a Stream. A Stream has mutation internally (caching). The internal mutation is not intended to surface on the interface, Stream is a good candidate for ReferentiallyTransperent annotation (though I am not sure how safe the synchronisation there is). Just my 2 cents.
Sadek

On Sun, Dec 12, 2010 at 1:56 PM, Philipp Dörfler <phdoerfler [at] googlemail [dot] com> wrote:
2010/12/9 Andreas Flierl <andreas [at] flierl [dot] eu>
Brian Goetz et al. are suggesting documenting a class' thread safety with annotations (@Immutable and @ThreadSafe or @NotThreadSafe) in their book "Java concurrency in practice". Maybe it would be worth considering doing this for the classes in the scala library (in the absence of an effect system).

+1 by me, too. Those annotations have proved to be useful in the past already.



--
www.sadekdrobi.com
ʎdoɹʇuǝ

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland