Eat, code, love.

Hi. I’m Fredrik Appelberg.

JavaScript Race Conditions

Yesterday I was rudely reminded that just because Node.js is single threaded doesn’t mean you don’t have to worry about race conditions.

Our architecture is heavily message-oriented and the code that handles ACKing messages looks something lite this. The idea is that application code can call either acknowledge(), which acknowledges that the task has been handled, or retry(), which means that the we should retry (a copy of) the task at a later time and then acknowledge that the original task has been dealt with. It should only be possible to retry or acknowledge a task once, which is why we keep track of the state in a variable @resolved.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Ack
    constructor: (@task, @queue) ->
        @resolved = false

    acknowledge: =>
        unless @resolved
            @task.acknowledge()
            @resolved = true

    retry: =>
        unless @resolved
            @queue.scheduleRetry(@task)
            .then =>
                @acknowledge()

Can you spot the error? It took me a while to realize that even though retry() makes sure that @resolved hasn’t been set and we call acknowledge() to ACK the task and set @resolved immediately afterwards, that actually happens asynchronously. We are using the Q promise library here, which means that if retry() is called repeatedly within the same process tick, then scheduleRetry() will almost certainly be called multiple times.

I think promises are wonderful, but the fact that they make asynchronous code look synchronous can really trick you. I’m pretty sure I wouldn’t have made this mistake if I’d written this piece in the regular old callback-oriented style.


Busy playing Hearthstone

So yeah, one reason I haven’t blogged or done any OSS work recently is I’ve been playing Hearthstone obsessively. I installed it with some trepidation and immediately got hooked so hard it is kind of scary. Now I can’t get enough. I read the blogs, watch the streams, follow the drama.

I’m strictly a casual player, and I’ve promised myself I won’t spend any actual money on this game. So in order to beef out my collection I’ve been grinding the Arena for the better part of two months. The Arena is pretty fun, as you get to play around with a lot of cards you wouldn’t usually get your hands on. It’s also punishing, as you really need to rack up those seven wins in order to recoup your gold investment. Last week I decided enough was enough, and switched to playing ranked games.

This season I was toiling away at level 20 for quite a while playing different decks, but then made a conscious commitment to learn the Shaman Way. Shaman is good for casuals like me since you can put together a solid deck out of just commons and rares, and it will both be fun to play and have a decent chance on the ladder. Playing Shaman correctly isn’t easy though; you have to pace yourself and learn when to strike with Lightning Storm, when to unleash your Feral Spirits, and what to hit with your Earthshock. And most importantly, when to hold back and just play a totem.

You also need to think hard about the mana curve. Despite being an all-round awesome card, I realized Chillwind Yeti had no place in my deck; on turn three I usually play an Overload card, which ruins turn four. After dropping the yetis, and a few other tweaks I was shocked how good the deck was doing. I went on win streak after win streak and hit rank 9 today. My one to two hours of game time a day isn’t enough to take me to Legend, but it was pretty sweet to at least break single digit.


My first board

I’m immensely proud of the fact that I soldered together my first prototype board last night. It holds two DS18B20s; one naked sensor soldered straight onto the board and one on a waterproof cable that I’ve hung out the window. This lets me measure current indoor and outdoor temperature.

The board is connected to my Raspberry Pi, where a node.js program using sensorjs is regularly sending the sensor data to a TempoDB database.


pi

Hardware is hard

Where I live there aren’t really any options for electronics shopping, so I was resigned to do it mostly through mail order. Fortunately I discovered that Kjell & Co., which is probably the closest thing to Radio Shack we have in Sweden, not only carries a lot of Arduino stuff, but also has an shop in the next town over (LuleĆ„, about 50km away).

So I got myself a soldering station, some lead-free solder1 some sensors and grab bag of diodes, resistors and cables to play with. It must be something like 25 years since I last had a soldering iron in my hands, but putting the T-cobbler together I all just came back to me. Even the smell was exactly as I remembered it.

I had some problems getting the DHT11 sensor up and running. All the examples I could find had a four-legged sensor, but mine came pre-mounted on a board with only three. Also, the configuration of the legs had been switched around. That one took some figuring out.

The DS18B20 however, just plain refused to show up as a device in /sys/bus/w1/devices. I tried everything: reflashed the SD card, checked the kernel messages, double-checked my connections (twice), moved things around on the bread board in case there was a break (which incidently there is; the vertical strips break in the middle, but this didn’t affect my wiring) and tried every combination of power and GND I could find on the breakout board in case I had done a bad soldering job (which I hadn’t). I did countless reboots. Still nothing.

I had just about written that sensor off when I discovered that the pullup resistor2 I’d used wasn’t yellow-purple-red as specified, but yellow-purple-yellow. 470K instead of 4.7K. Oops.

Hardware is hard.


  1. Which everyone seems to think is terrible, but I haven’t had any problems with. Yet.

  2. I confess I have no idea what a pullup resistor actually does.


Hello, World

Since I pretty much do node.js full-time nowadays, I was curious if I’d be able to run it on my Pi as well. Turns out that’s no big deal; there are binaries available for download so you don’t have to go through the hassle of compiling (which I hear can take quite some time).

Interfacing with the PiFace is another matter. There is a module called piface-node, but I couldn’t get it to do anything, and looking at the source code I could see that it was a bit too low-level for what I wanted to do. However, I did find a C library called libpifacecad, and started hacking to see if I could write a node wrapper around it.

After a few evenings worth of work I am at a point where I can read the sensors pretty reliably and writing to the screen somewhat less so (it works maybe one time out of three). That’s not the fault of the underlying library though, it seems pretty solid. Rather, I suspect there’s either a race condition somewhere (possibly), or I’m making assumptions about C++ memory management that’s not entirely true (probably), or I need to rethink how to do I/O in the node.js event loop (almost certainly).



The Joy of Hardware

Last year Jfokus ran an embedded systems track alongside the main conference, and this was met with considerable enthusiasm. It was a friendly refuge from corporate keynotes and enterprise bloatware seminars, a place where you could escape the buzzwords and marketing people. I discovered that the Raspberry Pi was an actual thing that existed and could be ordered from the internet, and within 60 minutes I had done just that.

Apart from putting together the odd PC back in the late 90’s, I’m generally clueless about hardware. I don’t know what the state of the art for RAM or CPUs are nowadays; in fact I don’t even know how many cores the MacBook I’m typing this on is running. Hardware is a nuisance; the problems that interest me all lie deep into software country.

However, the Raspberry Pi has awakened my curiosity for circuit boards, ports and capacitors. I want to learn about microcontrollers, I want to find out how GPIO works. God help me, I want to solder.

Karin from r-pi.se had a table at the conference, and seemed to be doing a brisk trade. This photo was taken during a lull in traffic, but usually it was so crowded you couldn’t even see the table.

I couldn’t resist getting a PiFace board; I’ve been wanting to add a display to my pi as I tend to run it headless and it would be nice to have some way of displaying status information. And adding a wi-fi dongle seemed like a good idea for a machine like this.

Admittedly, these are toys. Pure indulgence, a guilty pleasure, as I still don’t know what I will do with the Pi. It will be interesting finding out, though.


Emacs Protip: org-mode

Man, where do I start? org-mode is Emacs’ Killer Application. It started out as an very capable outlining tool, but has grown and mutated and now people use it for time management, blogging, presentations, spreadsheets, GTD, you name it. Org-mode is the reason I returned to Emacs after several years as an Eclipse refugee. I needed a system for handling my ToDos, and after hearing good things about org-mode I installed Emacs again and gave it a go. And here I am several years later and I pretty much live and breathe Emacs now.

In other words: if you haven’t tried org-mode, you’re probably missing out. Here, have a look at this screencast. It’s short and sweet and you know you want to.



Emacs Protip: ido-mode

If you are using emacs for anything more involved than editing config files every now and then you owe it to yourself try out a productivity enhancer like ido-mode. It lives in the minibuffer and provides way better completion for file names, emacs commands, buffers, you name it. Seriously, check it out if you haven’t already.

In all fairness, I should probably point out icicles as well as it seems to provide much of the same functionality as ido-mode. But since I’ve never used it I can’t really comment on it.