JVM issue: concurrency is affected by changing the date of the system! [part 4]

I am frequently challenged about the seriousness of this bug and the impact that it has. It’s not the first time I try to explain that, because this bug affects LockSupport.parkNanos() it basically spreads like a virus across all the platform, but let’s see this more practically

$ grep -r -l "parkNanos" .
./java/util/concurrent/Future.java
./java/util/concurrent/FutureTask.java
./java/util/concurrent/ThreadPoolExecutor.java
./java/util/concurrent/ScheduledThreadPoolExecutor.java
./java/util/concurrent/RunnableFuture.java
./java/util/concurrent/package-info.java
./java/util/concurrent/ExecutorCompletionService.java
./java/util/concurrent/CancellationException.java
./java/util/concurrent/RunnableScheduledFuture.java
./java/util/concurrent/CompletableFuture.java
./java/util/concurrent/AbstractExecutorService.java
./javax/swing/text/JTextComponent.java
./javax/swing/SwingWorker.java
./sun/swing/text/TextComponentPrintable.java
./sun/swing/SwingUtilities2.java

Well, it does not look that bad, does it? But uhm… I guess we are missing something… who’s using this classes? And who’s using such classes? And who’s using such classes? Omg… I am getting an headhache! Almost EVERYTHING is using this! So trust me, you will be affected. Maybe you are still not believing it, but please remember that this affects also Object:wait(:long) and so, transitively, also synchronized. Wait… WOOT? Oh yeah ๐Ÿ™‚ So lots of fun! Especially when your system, deployed on client premises, starts doing “strange” things and you are called by your (not very happy) support team.

Be aware that this bug is now fixed in JDK8 and I have no knowledge of any successful backports of it into JDK7.

See also
The full saga, all the articles I published on the matter:

JVM issue: concurrency is affected by changing the date of the system! [part 3]

I have been asked further information about the matter and for that reason I am pushing a bit of more code here. It’s C++, so be aware of it! For the records, we are looking at the sources of the hotspot JVM, you can find the source here:
http://hg.openjdk.java.net/jdk8/jdk8/hotspot/file/aed585cafc0d/src/os/linux/vm/os_linux.cpp

Let’s have a look at the park() function of PlatformEvent, which is used within all synchronization primitives of the JVM:

int os::PlatformEvent::park(jlong millis) {
   [...]
 
   struct timespec abst;
   compute_abstime(&abst, millis);
   [...]

   while (_Event < 0) {
     status = os::Linux::safe_cond_timedwait(_cond, _mutex, &abst);
     if (status != 0 && WorkAroundNPTLTimedWaitHang) {
       pthread_cond_destroy (_cond);
       pthread_cond_init (_cond, NULL) ;
     }
     assert_status(status == 0 || status == EINTR ||
                   status == ETIME || status == ETIMEDOUT,
                   status, "cond_timedwait");
     if (!FilterSpuriousWakeups) break ;                 // previous semantics
     if (status == ETIME || status == ETIMEDOUT) break ;
     // We consume and ignore EINTR and spurious wakeups.
   }

Please look at the line in bold, where the end time to wait is computed: if you open that function (line 5480) you will notice that it’s calculating an absolute time. based on the wall clock

   [...]
   static struct timespec* compute_abstime(timespec* abstime, jlong millis) {

      if (millis < 0)  millis = 0;

      struct timeval now;
      int status = gettimeofday(&now, NULL);
      [...]

So what will happen is that the park function will be waiting on an absolute time based on a wall clock, hence will fail miserably if the wall clock is changed.

The simplest fix, without changing too much code, would be to use the CLOCK_MONOTONIC (or CLOCK_MONOTONIC_RAW, even better) to compute the absolute time ( clock_gettime(CLOCK_MONOTONIC, &ts) ) and also to check it the same way in the main loop (you can associate any available clock with a pthread_cond_timewait)

Then, if we really want to stay on the safe side, we should avoid using absolute delays and use relative delays, as POSIX specs explicitly guarantees that threads waiting on a relative time are not affected to changes to the underling clock, while when using absolute delays the situation is historically “fuzzy”.

Is that complex? I does not look so, at least looking at the code (I will try to patch it myself for sure) but I surely do not grasp the complexity of the whole hotspot, so I may fail miserably. It also have to be noted that my C++ skills are kind of dated ๐Ÿ™‚

See also
The full saga, all the articles I published on the matter:

JVM issue: concurrency is affected by changing the date of the system! [part 2]

Based on a lot of questions I received in various mailing lists related to the previous post and in order to make the issue simpler and clearer I decided to go back to a binary deliverable (code) that shows the problem, hope this helps!

This is my PreciousPool class, that handles Precious resources:

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PreciousPool {

    public static class Precious {
        private final int id;

        private Precious() {
            this.id = 100+(int)(Math.random()*900.0);
        }

        public String toString() {
            return "Precious n."+id;
        }
    }

    private final Lock lock;
    private final Condition ready;
    private final long timeoutInMillis;

    private final List preciousLended;
    private final List preciousAvailable;

    public PreciousPool(int size, long timeoutInSeconds) {
        this.lock = new ReentrantLock();
        this.ready = lock.newCondition();

        this.timeoutInMillis = 1000L*timeoutInSeconds;
        this.preciousLended =  new ArrayList();
        this.preciousAvailable = new ArrayList();

        for (int i = 0; i < size; i++) {
            preciousAvailable.add(new Precious());
        }
    }

    public Precious obtain()  {
        lock.lock();
        try {
            // if no precious are available we wait for the specified timeout (releasing the lock so that others can try)
            if (preciousAvailable.size() == 0) {
                try {
                    ready.await(timeoutInMillis, TimeUnit.MILLISECONDS);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw new RuntimeException("Somebody interrupted me!", e);
                }
            }

            // if a precious is available we unload it and return to the caller, otherwise null
            if (preciousAvailable.size() > 0) {
                Precious value = preciousAvailable.remove(0);
                preciousLended.add(value);
                return value;
            } else {
                return null;
            }
        } finally {
            lock.unlock();
        }
    }

    public void release(Precious value) {
        lock.lock();
        try {
            if (!preciousLended.remove(value))
                throw new RuntimeException("Element "+value+" was not lended!");

            // if a precious is returned we put it back and signal to anybody waiting
            preciousAvailable.add(value);
            ready.signalAll();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String args[]) {
        final int size = 3;
        final PreciousPool pool = new PreciousPool(size, 5);

        // let's exhaust the pool
        for (int i=0; i<size; i++)
            dump(pool.obtain());

        // and as we are stubborn we continuosly ask for a new one
        while(true) {
            dump(pool.obtain());
        }
    }

    private static void dump(Precious precious) {
        if (precious == null)
            log("I did not get my precious :(");
        else
            log("I did get my precious! "+precious);
    }

    private static void log(String message) {
        final String now = new SimpleDateFormat("HH:mm:ss:SSSS ").format(new Date());
        System.out.println(now + message);
    }
}

So, the main is a single thread (no need for multithreading here, let’s keep it simple), that first exhaust the whole pool and then keep asking, without success, for a resource. Stubborn guy, I say, but it happens. If you run this program everything works as expected: you are greeted by a three successful Precious and then an endless list of failures, that it continuously grow. All good ๐Ÿ™‚

02:34:40:0061 I did get my precious! Precious n.156
02:34:40:0062 I did get my precious! Precious n.991
02:34:40:0062 I did get my precious! Precious n.953
02:34:45:0064 I did not get my precious!
02:34:50:0065 I did not get my precious!
02:34:55:0066 I did not get my precious!
02:35:00:0067 I did not get my precious!
02:35:05:0068 I did not get my precious!
[...]

But guess what happens when, while the program is running, I change the date of my system back of one hour? Everything stops, it’s simple as that. No prints, nothing, zero, nada. Now, If it wasn’t so late, I would probably wait one hour in order to have my program restored to his normal process, but as a customer I won’t be terribly happy ๐Ÿ™‚

See also
The full saga, all the articles I published on the matter:

JVM issue: concurrency is affected by changing the date of the system!

Executive summary
The implementation of the concurrency primitive LockSupport.parkNanos(), the function that controls *every* concurrency primitive on the JVM, is flawed, and any NTP sync, or system time change backwards, can potentially break it with unexpected results across the board when running a 64bit JVM on Linux 64bit

What we need to do?
This is an old issue, and the bug was declared private. I somehow managed to have the bug reopened to the public, but it’s still a P4, that means that probably won’t be fixed. I think we need to push for a resolution ASAP, be sure that’s in for JDK9, make all the possible effort to make this fix for JDK8 or, at least, to include it in a later patch release. In an ideal world it would be nice to have a patch for JDK7

Why all this urgency?
If a system time change happens then all the threads parked will hang, with unpredictable/corrupted/useless results to the end user. Same applies to Future, Queue, Executor, and any other construct that it’s somehow related to concurrency. This is a big issue for us and for any near time application: please think about trading and betting, where the JVM is largely used. And please do not restrain yourself to the Java language: add Scala and any other JVM-based language to the picture.

All the details (spoiler: tech stuff!)
To be more clear about the issue, the extent of it and the concurrency library, let me introduce this very simple program:

import java.util.concurrent.locks.LockSupport;

public class Main {
  public static void main(String[] args) {
    for (int i=100; i>0; i--) {
      System.out.println(i);
      LockSupport.parkNanos(1000L*1000L*1000L);
    }
    System.out.println("Done!");
  }
}

Run it with a 64bit 1.6+ JVM on 64bit Linux, turn the clock down one hour and wait until the counter stops… magic! I tested this on JDK6, JDK7 and latest JDK8 beta running on various Ubuntu distros. It’s not just a matter of (old?) sleep() and wait() primitives, this issue it affects the whole concurrency library.

To prove that this is fixable, I reimplemented the program above above substituting LockSupport.parkNanos() with a JNI call toclock_nanosleep(CLOCK_MONOTONIC…): works like a charm ๐Ÿ˜ฆ
This is due to the fact that the CPP code is calling the pthread_cond_timedwait() using its default clock (CLOCK_REALTIME) which, unfortunately is affected by settime()/settimeofday() calls (on Linux): for that reason it cannot be used to measure nanoseconds delays, which is what the specification requires. CLOCK_REALTIME is not guaranteed to monotonically count as this is the actual “system time”: each time my system syncs time using a NTP server on the net, the time might jump forward or backward. The correct call (again on Linux) would require to use CLOCK_MONOTONIC as clock id, which are defined by POSIX specs since 2002. (or better CLOCK_MONOTONIC_RAW)

The POSIX spec is infact clear, as it states “…setting the value of the CLOCK_REALTIME clock via clock_settime() shall have no effect on threads that are blocked waiting for a relative time service based upon this clock…”: it definitely states “relative”. Having a look at the hotspot code, it appears that the park() is using compute_abstime() (which uses timeofday) and then waits on an absolute period: for that reason it’s influenced by the system clock change. Very wrong.

Next steps?
I am trying to raise the awareness of this issue, basically involving as much people as I can. I will continue to do that and escalate as soon as I get some solid response from Oracle. I am also working on a patch myself.

See also
The full saga, all the articles I published on the matter:

Flash Plugin for Ubuntu 64

Long time since last post, but at least a useful bit of information. On Ubuntu 11.04 64bit if you install the flashplugin-installer, either alone or as part of the metapackage ubuntu-restricted-extras you will get…. surprise, a 32bit flaash plugin! Very annoying, also because it does not work very well.

Just install adobe-flushplugin instead: it will uninstall the old version and you will get a proper 64bit version ๐Ÿ™‚

DMRAID on Ubuntu with SATA fakeraid

The problem
Ubuntu (and Debian) doesn’t have support for the SATA RAID (also called FakeRAID since some of the functionality is provided by software) controllers being shipped on recent motherboards. The software to handle FakeRAID arrays in Linux is DMRAID. How do I install such systems?

.
Preliminary Operations
You will need to configure your RAID using the bios utility (Ctrl+M for me on startup) : just select your disks, create the logical unit selecting your desired raid mode (0 or 1 for me), save and boot with a desktop live CD.

Note: in this example I’m using Fujitsu Siemens Primergy RX100 S4 equipped with two 160Gb SATA drives, and I’m creating a RAID0 array, with Ubuntu Linux 7.10 Gutsy Gibbon

.
1st phase – create your partitions

To create your partitions you’ll need that your RAID is reconized from your system. Unfortunately, at the time I’m writing, Ubuntu does not include DMRAID support in its install procedure, so you’ll need to start with a live CD, activate DMRAID support and proceed to partitioning.

  • start your system with an Ubuntu Desktop CD (select your language if needed)
  • open a terminal (CTRL-ALT-F1), set a new root password wih “sudo passwd root”
  • go on with “su – ” (digit your password)
  • edit /etc/apt/sources.list, uncomment universe packages
  • “apt-get update”
  • install dmraid: “apt-get install dmraid”
  • Launch fdisk specifying the shorter device you find under /dev/mapper/dxxx (dxxx is the file device with shorted name, es.: dxxxa28 is the correct one, dxxxa281 the wrong one)
  • create a primary partition of 316GB, the other with the remaining space (we’ll use it for swap)
    • n <enter>
    • p <enter>
    • 1<enter>
    • <enter>
    • +316GB
  • create a primary partition with the remaining space:
    • n <enter>
    • p <enter>
    • 2 <enter>
    • <enter>
    • <enter>
  • change second partition type:
    • t <enter>
    • 2 <enter>
    • 82 <enter>
  • save with “w”, <enter>
  • restart your system with “reboot”

.

2nd phase – installing a minimal ubuntu
To format your partitions you’ll need DMRAID module loaded. So you’ll need to repeat the first six previous steps.

  • format your root partition
    • mkfs -t ext3 /dev/mapper/dxxx1 (select the device ending with 1)
  • prepare a local area for the installation procedure: create a local folder, mount your formatted partition on that folder, bind and mount the necessary stuff
    • mkdir /target
    • mount /dev/mapper/dxxx1 /target
    • mkdir /target/dev
    • mountย  – – bind /dev/ /target/dev
    • mkdir /target/proc
    • mount -t proc proc /target/proc
    • mkdir /target/sys
    • mount -t sysfs sys /target/sys
  • proceed installing a minimal ubuntu
    • apt-get install debootstrap
    • debootstrap gutsy /target
  • copy essential files on your partition
    • cp /etc/apt/sources.list /target/etc/apt/
    • cp /etc/resolv.conf /target/etc/
    • cp /etc/hosts /target/etc/
    • cp /etc/network/interfaces /target/etc/network/
  • switch on your new system, re-mount /proc, /sys, /dev
    • mountย  – – bind /dev/ /target/dev
    • mount -t proc proc /target/proc
    • mount -t sysfs sys /target/sys
    • chroot /target
  • install a basic ubuntu system
    • apt-get update
    • apt-get install language-pack-en
    • apt-get install ubuntu-standard linux-generic dmraid grub
  • create a user, if you want
    • useradd pippo
    • passwd pippo
    • mkdir /home/pippo
    • chown pippo /home/pippo
  • set root password
    • passwd root

.

3rd phase – boot loader configuration
Yes, you’ll need to configure grub

  • copy grub essential files
    • mkdir /boot/grub
    • cp /usr/lib/grub/i386-pc/stage1 /boot/grub/
    • cp /usr/lib/grub/i386-pc/stage2 /boot/grub/
    • cp /usr/lib/grub/-pc/* /boot/grub/
      This will copy the staging file for the various filesystem in your boot partition. In my example the directory is โ€œ/usr/lib/grub/i386-pc/โ€ and the files that are copied are โ€œe2fs_stage1_5โ€ณ, โ€œjfs_stage1_5โ€ณ,โ€ฆ
  • configure di grub
    • grub (enters grub interactive shell)
    • device (hd0) /dev/mapper/dxxx (the shorter, so the logical disk, not the partition)
    • root (hd0,0)
    • setup (hd0)
    • quit (exit from grub shell)
    • update-grub (update grub files)
  • configure grub menu
    • edit grub menu.lst: “vi /boot/grub/menu.lst”
    • check groot is pointing to hd0,0 (it should be necessary to uncomment that line)
    • check that savedefault is commented (normally is)
    • modify, inside “kernel” items, the “root” value to “/dev/mapper/dxxx1” (your root partition)
  • configure static mount under /etc/fstab

# /etc/fstab: static file system information.
#
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
/dev/mapper/ddf1_LogicalDrive_01 / ext3 defaults 0 1
/dev/mapper/ddf1_LogicalDrive_02 none swap sw 0 0

  • reboot ๐Ÿ™‚

Bottom line
Well, you’ve done! Remember that if you mounted a RAID0 device Linux at startup will probe your disks and he’ll wrongly detect a bad partition table: ignore error messages http://osdir.com/ml/linux.ataraid/2006-10/msg00019.html

Little tips:

Links
http://www.ubuntu-in.org/wiki/SATA_RAID_Howto
https://help.ubuntu.com/community/FakeRaidHowto
http://salaros.blogspot.com/2007/07/installare-ubuntu-704-su-raid0-in-dual.html