Race conditions in Javascript

Given all hype about javascript recently, it can come as a shock to realise that modern browsers still execute javascript on a single thread. For developers used to multi-threaded runtimes such as .NET and the JVM this can lead to unfamiliar results.

For this example we will consider the HTML 5 Websockets API.

Note first of all that there is no connect() method with websockets, the constructor itself initiates the connection.

Let us consider the following code. By adding a big loop we’ve essentially added a time delay between creating the websocket and wiring up the onopen event handler. Now the million dollar question: does the event handler always get triggered when a connection is made, or does the delay mean that the onopen handler is sometimes not wired up by the time the websocket has connected?

var wsUri, testWebSocket;
wsUri = "ws://localhost:8080/mywebsocket";
testWebSocket = function(){
  var connection, i$, i;
  connection = new WebSocket(wsUri);
  for (i$ = 0; i$ <= 1000000000; ++i$) {
    i = i$;
    /* simulate a delay */
  }
  console.log("wiring up event handler");
  connection.onopen = function(){
    console.log("opened");
  };
};
testWebSocket();

In the JVM/.NET world we would expect this to cause a race condition. If the websocket connects before our event handler has been wired up the handler would never get triggered. In fact though, this works fine in javascript and there is no race condition. Since the execution model is single-threaded the websocket doesn’t actually attempt to connect until nothing else is executing. Very much like a Dispatcher in WPF/Silverlight.

Of course you do still have to be very careful when using timers… this for example does indeed cause a race condition:

var wsUri, testWebSocket;
wsUri = "ws://localhost:8080/mywebsocket";
testWebSocket = function(){
  var connection, wireUp;
  connection = new WebSocket(wsUri);
  wireUp = function(){
    console.log("wiring up event handler");
    connection.onopen = function(){
      console.log("opened");
    };
  };
  window.setTimeout(wireUp, 5000);
};
testWebSocket();

Remember folks: Concurrency and asynchrony are not the same thing!

Personally I think this API design is a bit short-sighted… it’s going to make it very difficult to add real concurrency to javascript runtimes and browsers in the future without revamping the APIs and breaking existing apps.

See also:
http://ejohn.org/blog/how-javascript-timers-work/

Advertisements

Installing Monodevelop 3 with F# support on Ubuntu

After much experimentation and digging around on google groups (special thanks to Ibrahim Hadad) I have finally managed to get Monodevelop 3 and F# working together nicely on Ubuntu. These were the steps I took. Your mileage may vary. 🙂

(Update: Knocte has suggested a couple of modifications to simplify the process. These are now reflected below.)

1) sudo apt-get install mono-complete libgdiplus git autoconf libtool

2) Install monodevelop using the script from John Ruiz’ blog:
http://blog.johnruiz.com/2012/05/installing-monodevelop-3-on-ubuntu.html

3) Get F# source and compile:
git clone git://github.com/fsharp/fsharp
cd fsharp/
./autogen.sh --prefix=/usr
make
sudo make install

4) Run monodevelop. Go to tools, add-in manager, gallery. Install F# language binding.

5) Enjoy!

Monodevelop 3 with F# bindings

Winning!

Debugging Silverlight applications with WinDbg

To use WinDbg to examine a dump…

1)      Make sure that the dump file is 32bit if it was running under a 32bit Silverlight runtime. Process Explorer creates 64 bit dumps on 64bit machines even for 32 bit applications. These will not work in WinDbg. You can use the Sysinternals procdump tool to create a 32 bit dump: procdump –ma sllauncher.exe mydump.dmp

2)      Make sure you are using the 32 bit version of WinDbg (for 32 bit dumps).

3)      Configure symbols in windbg: .sympath SRV*c:\symbolcache*http://msdl.microsoft.com/download/symbols

4)      Load the dump file in WinDbg (File, Open crash dump)

5)      Load SOS and the CoreCLR for Silverlight. The .loadby command seems to be broken so you’ll have to use .load and enter the complete paths:


.load C:\Program Files (x86)\Microsoft Silverlight\5.1.10411.0\sos.dll
.load C:\Program Files (x86)\Microsoft Silverlight\5.1.10411.0\coreclr.dll

If you’re using the 64 bit Silverlight runtime I believe you just need to use the 64bit WinDbg and load the dlls from C:\Program Files\Microsoft Silverlight\5.1.10411.0 instead.

You should be ready to go, for example:

0:000> !clrstack
OS Thread Id: 0x45dc (0)
Child SP IP Call Site
0014f3c0 03aa025f SilverlightApplication2.MainPage..ctor()
0014f3cc 03aa0215 SilverlightApplication2.App.Application_Startup(System.Object, System.Windows.StartupEventArgs)
0014f3e4 7b316fa3 MS.Internal.CoreInvokeHandler.InvokeEventHandler(UInt32, System.Delegate, System.Object, System.Object)
0014f410 7b2f5239 MS.Internal.JoltHelper.FireEvent(IntPtr, IntPtr, Int32, Int32, System.String, UInt32)
0014f460 7b390969 DomainNeutralILStubClass.IL_STUB_ReversePInvoke(Int32, Int32, Int32, Int32, IntPtr, Int32)
0014f510 02e017a7 [ContextTransitionFrame: 0014f510]

Sometimes, for example if your Silverlight application uses managed .NET COM components, WinDbg will try to load the wrong CLR debugging module. The quote from the deep dark depths of the WinDBG help file:

“To debug a managed application, the debugger must load a data access component (DAC) that corresponds to the CLR that the application has loaded. However, in some cases, the application loads more than one CLR. In that case, you can use the I parameter to specify which DAC the debugger should load.”

In this case the following two commands should sort things out:


.cordll -u
.cordll -I coreclr -lp "C:\Program Files (x86)\Microsoft Silverlight\5.1.10411.0"

This article also may also be useful:

http://www.codeproject.com/Articles/331050/Assembly-Helps-Debug-NET-Applications

Derivatives of a polynomial in F#

Just because I was bored…

type Term = {Coefficient: int; Power: int}
type Term = {Coefficient: int; Power: int}
type Polynomial = list<Term>

let derivative polynomial =
    polynomial |> List.map (fun term -> {Coefficient = term.Coefficient * term.Power; Power = term.Power - 1}) |> List.filter (fun x -> x.Power >= 0)

let prettyPrint polynomial =
    let sortedTerms = polynomial |> List.sortBy (fun term -> -term.Power)

    let rec printTerms terms =
        match terms with
        | [] -> ""
        | [term] ->
            match term.Power with
            | 0 -> sprintf "%d" term.Coefficient
            | 1 -> sprintf "%dx" term.Coefficient
            | n -> sprintf "%dx^%d" term.Coefficient term.Power                           
        | h::t -> printTerms [h] + " + " + printTerms t

    printTerms sortedTerms

let sample = [{Coefficient=3; Power=2};{Coefficient=5; Power=1};{Coefficient=4;Power=0}]
let derived = sample |> derivative

printfn "Polynomial:\t%s" <| prettyPrint sample
printfn "Derivative:\t%s" <| prettyPrint derived

 

Town Crier 1.1 now available on NuGet

Town Crier can now be downloaded from NuGet 🙂

There is now also some built in markdown support (thanks to friism). This provides a convenient way to send HTML emails where possible but with a human readable plaintext alternative whilst only writing one template:

            var factory = new MergedEmailFactory(new TemplateParser());

            var tokenValues = new Dictionary<string, string>
                                  {
                                      {"name", "Joe"},
                                      {"userid", "123"}
                                  };

            MailMessage message = factory
                .WithTokenValues(tokenValues)
                .WithSubject("Test Subject")
                .WithMarkdownBodyFromFile(@"templates\sample-markdown.txt")
                .Create();

To install Town Crier into your project from the Visual Studio Package Console:
install-package towncrier

Temporary file helper class

Occasionally it’s necessary to output data into a temporary file, for example in order to pass data to an external program. I threw together this little helper class to help out in such situations.

public class TemporaryFile : IDisposable
{
    public string FilePath { get; protected set; }

    public TemporaryFile()
    {
        FilePath= Path.GetTempFileName();
    }

    public void Dispose()
    {
        if (File.Exists(FilePath))
            File.Delete(FilePath);
    }
}

Use it like this:

using (var tempInputFile = new TemporaryFile())
{
   // Do stuff with tempInputFile.FilePath here...
}

// Dispose will be called at the end of the using statement and so the file will be deleted.

Implementing map-reduce in F#

Introduction

MapReduce is a software paradigm popularised by Google in which we take a set of tuples (key-value pairs), transform (map) them into an intermediate set of key-value pairs, and then perform some aggregation (reduce) operation on the intermediate values to obtain a result set. This is a useful way to express a problem because it yields an obvious way to “divide and conquer” the computation in a way that lends itself to parallel/distributed computing, thus providing a fairly simple way to perform computations on extremely large data sets.

It can be quite difficult to grok at first, so I decided to try implementing one of the examples from the MongoDB documentation in F# (if interested, see shell example 2). In this example, we have a list of people and the types of pet each of them has. We wish to calculate the total number of each animal.

The Code

Again, F# proves to be a remarkably succinct language to express problems, in this case the built in syntactic sugar for tuples is a godsend!

UPDATE (25-May-2010) – Controlflow helpfully suggested that I could make my original code somewhat neater by using pattern matching to decompose tuples. I’ve updated the code below with these improvements.

#light

// Simple example of map-reduce  in F#
// Counts the total numbers of each animal

// Map function for our problem domain
let mapfunc (k,v) =
    v |> Seq.map (fun(pet) -> (pet, 1))

// Reduce function for our problem domain
let reducefunc (k,(vs:seq<int>)) =
    let count = vs |> Seq.sum
    k, Seq.ofList([count])

// Performs map-reduce operation on a given set of input tuples
let mapreduce map reduce (inputs:seq<_*_>) =
    let intermediates = inputs |> Seq.map map |> Seq.concat
    let groupings = intermediates |> Seq.groupBy fst |> Seq.map (fun(x,y) -> x, Seq.map snd y)
    let results = groupings |> Seq.map reduce
    results

// Run the example...
let alice = ("Alice",["Dog";"Cat"])
let bob = ("Bob",["Cat"])
let charlie = ("Charlie",["Mouse"; "Cat"; "Dog"])
let dennis = ("Dennis",[])

let people = [alice;bob;charlie;dennis]

let results = people |> mapreduce mapfunc reducefunc

for result in results do
    let animal = fst result
    let count = ((snd result) |> Seq.toArray).[0]
    printfn "%s : %s" animal (count.ToString())

printfn "Press any key to exit."

System.Console.ReadKey() |> ignore

This yields the expected results:

Dog : 2

Cat : 3

Mouse : 1

Exercise for the reader

Parallelise this implementation (for a single machine this should be trivial by using the Parallel LINQ integration provided in the F# Powerpack).