Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

LAFuture.collect crashes when futures are resolved out of order #1453

Merged
merged 1 commit into from

2 participants

@tuhlmann
Collaborator

I'm using LAFuture.collect from Lift 3 Snapshot to collect 3 futures into one. It sometimes throws an IndexOutOfBoundsException at me.
The reason is this line:

val vals = new collection.mutable.ArrayBufferBox[T]

which defines the result array. Later, when future 2 is satisfied before future 1 it tries to insert it at a index that does not yet exist, thus throwing the exception.
The fix I found was to pad the array with Empty's and then later in the foreach write a Full into it.

Here's the repl that produces the exception:

scala> new collection.mutable.ArrayBufferInt
res0: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

scala> res0.insert(2, 4)
java.lang.IndexOutOfBoundsException: 2

@tuhlmann tuhlmann was assigned
@tuhlmann
Collaborator

Any opinions?

@dpp
Owner

Ship it

@dpp dpp merged commit 6363f72 into from
@dpp dpp deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 25, 2013
  1. @tuhlmann
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 6 deletions.
  1. +10 −6 core/actor/src/main/scala/net/liftweb/actor/LAFuture.scala
View
16 core/actor/src/main/scala/net/liftweb/actor/LAFuture.scala
@@ -136,7 +136,9 @@ object LAFuture {
def collect[T](future: LAFuture[T]*): LAFuture[List[T]] = {
val sync = new Object
val len = future.length
- val vals = new collection.mutable.ArrayBuffer[T](len)
+ val vals = new collection.mutable.ArrayBuffer[Box[T]](len)
+ // pad array so inserts at random places are possible
+ for (i <- 0 to len) { vals.insert(i, Empty) }
var gotCnt = 0
val ret = new LAFuture[List[T]]
@@ -144,10 +146,10 @@ object LAFuture {
case (f, idx) =>
f.foreach {
v => sync.synchronized {
- vals.insert(idx, v)
+ vals.insert(idx, Full(v))
gotCnt += 1
if (gotCnt >= len) {
- ret.satisfy(vals.toList)
+ ret.satisfy(vals.toList.flatten)
}
}
}
@@ -166,7 +168,9 @@ object LAFuture {
def collectAll[T](future: LAFuture[Box[T]]*): LAFuture[Box[List[T]]] = {
val sync = new Object
val len = future.length
- val vals = new collection.mutable.ArrayBuffer[T](len)
+ val vals = new collection.mutable.ArrayBuffer[Box[T]](len)
+ // pad array so inserts at random places are possible
+ for (i <- 0 to len) { vals.insert(i, Empty) }
var gotCnt = 0
val ret = new LAFuture[Box[List[T]]]
@@ -176,10 +180,10 @@ object LAFuture {
vb => sync.synchronized {
vb match {
case Full(v) => {
- vals.insert(idx, v)
+ vals.insert(idx, Full(v))
gotCnt += 1
if (gotCnt >= len) {
- ret.satisfy(Full(vals.toList))
+ ret.satisfy(Full(vals.toList.flatten))
}
}
Something went wrong with that request. Please try again.