- A Concurrent Affair - https://www.concurrentaffair.org -

Mac OS X Race Condition in DrJava Start-Up

A few days ago, I noticed that double-clicking on a *.java file in Windows Explorer caused an AssertionError [1] in DrJava. This wasn’t a huge issue, because it only shows with assertions enabled (i.e. -ea in the JVM Arguments for the Main JVM).

It’s still something I wanted to fix, because under some circumstances, opening a file this way could violate the convention of accessing certain Swing data structures only in the event thread and cause problems down the road. So, after being done with our Mint paper and taking the weekend off [2], I made the simple change of moving code into an invokeLater.

But when testing it, I discovered that Finder integration on the Mac wasn’t working [3] anymore. It turned out this was also an effect of having assertions enabled with the -ea argument: When a file is opened from the Finder, the ApplicationListener.handleOpenFile [4] method is called, but when we have Main JVM arguments like -ea, we need to restart DrJava, and the second instance doesn’t get another call to handleOpenFile anymore, so we need to change the file from that method into a command line parameter.

Whether this works or not depends on when the call to handleOpenFile arrives: Does it arrive before or after the restart? This is a race condition.

Unfortunately, fixing this properly is difficult. It seems like it is guaranteed that handleOpenFile will be called eventually if the file was opened from the Finder, but we don’t know when. How long do we wait before we decide to not wait any longer and restart? It would have been nice to get a handleOpenFileComplete call after all events (there can be multiple) have been processed. That method should be called as well if no calls to handleOpenFile are generated. But that’s not how Mac OS X does it.

I restart now whenever DrJava gets ready, and then uses the DrJava remote control [5] to open files that arrive too late. Of course, that only works if remote control is enabled and if the new instance of DrJava can is actually running a remote control server.

Some big ifs, but I think this is the best I can do now without a lot of re-engineering.

[6] [7]Share [8]