[bisq-network/bitcoinj] jdk8 vs jdk 10 (#17)

Oscar Guindzberg notifications at github.com
Thu Dec 27 15:40:43 UTC 2018


- I created a windows box on aws to test stuff on it.
- The workaround of building bitcoinj with jdk8 and bisq with jdk10 works fine at build time. At runtime, everything is run with java 10, so there is still a problem.
- Opening and and closing the Bisq app on windows works smoothly.
  - My guess: During "bitcoinj shutdown", NoSuchMethodError is thrown when sun.misc.Cleaner code is going to be executed by WindowsMMapHack but since WalletConfig.shutdown() has a “catch Throwable” that does not do a log, nothing is printed on the log. Since the app closes, maybe locks are cleaned up and the problem is not exposed to the user.
- Bisq restore from seed on windows fails. 
  - My guess: NoSuchMethodError is thrown and not logged (same thing that happens when Bisq is closed).  Then when a “new bitcoinj” is created, we get an exception. 
  - The UI informs the user an error has ocurred. The log prints: 
```
Dec-26 19:26:50.393 [JavaFX Application Thread] INFO  o.b.c.MnemonicCode: PBKDF2 took 14.38 ms
Dec-26 19:26:50.632 [ STOPPING] INFO  o.b.c.PeerGroup: Awaiting PeerGroup shutdown ...
Dec-26 19:26:50.633 [PeerGroup Thread] INFO  o.b.c.PeerGroup: Stopping ...
Dec-26 19:26:51.063 [PeerGroup Thread] INFO  o.b.c.PeerGroup: Stopped.
Dec-26 19:26:51.615 [ STOPPING] INFO  o.b.s.SPVBlockStore: Windows mmap hack: Forcing buffer cleaning
Dec-26 19:26:51.766 [RestoreBTCWallet-%d] INFO  b.c.b.s.WalletsSetup: Socks5Proxy for bitcoinj: socks5Proxy=127.0.0.1:50495     Version 5
Dec-26 19:26:51.775 [RestoreBTCWallet-%d] INFO  o.b.c.Context: Creating bitcoinj 0.14.4.11 context.
Dec-26 19:26:51.777 [RestoreBTCWallet-%d] INFO  b.c.b.n.BtcNetworkConfig: You connect with peerAddresses: [[r3dsojfhwcm7x7p6.onion]:8333, [vlf5i3grro3wux24.onion]:8333, [fz6nsij6jiyuwlsc.onion]:8333, [c6ac4jdfyeiakex2.onion]:8333, [sjyzmwwu6diiit3r.onion]:8333, [3xucqntxp5ddoaz5.onion]:8333, [lgkvbvro67jomosw.onion]:8333, [4jyh6llqj264oggs.onion]:8333, [mxdtrjhe2yfsx3pg.onion]:8333, [3r44ddzjitznyahw.onion]:8333, [i3a5xtzfm4xwtybd.onion]:8333, [z33nukt7ngik3cpe.onion]:8333, [sslnjjhnmwllysv4.onion]:8333]
Dec-26 19:26:51.785 [ STARTING] INFO  b.c.b.s.WalletConfig: Wallet directory: C:\Users\Administrator\AppData\Roaming\Bisq\btc_mainnet\wallet
Dec-26 19:26:51.788 [ STARTING] INFO  b.c.b.s.WalletConfig: Renaming old wallet file C:\Users\Administrator\AppData\Roaming\Bisq\btc_mainnet\wallet\bisq_BTC.wallet to C:\Users\Administrator\AppData\Roaming\Bisq\btc_mainnet\wallet\Backup 1 for bisq_BTC.wallet
Dec-26 19:26:51.827 [ STARTING] INFO  o.b.w.DeterministicKeyChain: 133 keys needed for M/44H/0H/0H/0 = 0 issued + 100 lookahead size + 33 lookahead threshold - 0 num children
Dec-26 19:26:51.878 [ STARTING] INFO  o.b.w.DeterministicKeyChain: Took 46.38 ms
Dec-26 19:26:51.878 [ STARTING] INFO  o.b.w.DeterministicKeyChain: 133 keys needed for M/44H/0H/0H/1 = 0 issued + 100 lookahead size + 33 lookahead threshold - 0 num children
Dec-26 19:26:51.918 [ STARTING] INFO  o.b.w.DeterministicKeyChain: Took 39.75 ms
Dec-26 19:26:52.012 [ STARTING] INFO  b.c.b.s.WalletConfig: Renaming old wallet file C:\Users\Administrator\AppData\Roaming\Bisq\btc_mainnet\wallet\bisq_BSQ.wallet to C:\Users\Administrator\AppData\Roaming\Bisq\btc_mainnet\wallet\Backup 1 for bisq_BSQ.wallet
Dec-26 19:26:52.013 [ STARTING] INFO  o.b.w.DeterministicKeyChain: 133 keys needed for M/44H/142H/0H/0 = 0 issued + 100 lookahead size + 33 lookahead threshold - 0 num children
Dec-26 19:26:52.034 [ STARTING] INFO  o.b.w.DeterministicKeyChain: Took 19.19 ms
Dec-26 19:26:52.034 [ STARTING] INFO  o.b.w.DeterministicKeyChain: 133 keys needed for M/44H/142H/0H/1 = 0 issued + 100 lookahead size + 33 lookahead threshold - 0 num children
Dec-26 19:26:52.063 [ STARTING] INFO  o.b.w.DeterministicKeyChain: Took 28.23 ms
Dec-26 19:26:52.090 [JavaFX Application Thread] ERROR b.c.b.s.WalletsSetup: Service failure from state: STARTING; failure={} java.io.IOException: org.bitcoinj.store.BlockStoreException: java.nio.channels.OverlappingFileLockException
        at bisq.core.btc.setup.WalletConfig.startUp(WalletConfig.java:494)
        at com.google.common.util.concurrent.AbstractIdleService$DelegateService$1.run(AbstractIdleService.java:62)
        at com.google.common.util.concurrent.Callables$4.run(Callables.java:122)
        at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: org.bitcoinj.store.BlockStoreException: java.nio.channels.OverlappingFileLockException
        at org.bitcoinj.store.SPVBlockStore.<init>(SPVBlockStore.java:133)
        at bisq.core.btc.setup.WalletConfig.provideBlockStore(WalletConfig.java:331)
        at bisq.core.btc.setup.WalletConfig.startUp(WalletConfig.java:406)
        ... 3 common frames omitted
Caused by: java.nio.channels.OverlappingFileLockException: null
        at java.base/sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)
        at java.base/sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152)
        at java.base/sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:1191)
        at java.base/java.nio.channels.FileChannel.tryLock(FileChannel.java:1160)
        at org.bitcoinj.store.SPVBlockStore.<init>(SPVBlockStore.java:106)
        ... 5 common frames omitted
Dec-26 19:26:52.097 [JavaFX Application Thread] ERROR b.d.u.GUIUtil: java.io.IOException: org.bitcoinj.store.BlockStoreException: java.nio.channels.OverlappingFileLockException
```
- I isolated and reproduced that behavior:
``` 
import sun.misc.Cleaner;
import sun.nio.ch.DirectBuffer;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class WindowsTest2 {
    public static void main(String[] argv) throws Exception {
        File myfile = new File("myfile.txt");
        boolean exists = myfile.exists();
        RandomAccessFile randomAccessFile = new RandomAccessFile(myfile, "rw");
        long fileSize = getFileSize();
        if (!exists) {
            //log.info("Creating new SPV block chain file " + file);
            randomAccessFile.setLength(fileSize);
        } else if (randomAccessFile.length() != fileSize) {
            throw new RuntimeException("File size on disk does not match expected size: " +
                    randomAccessFile.length() + " vs " + fileSize);
        }

        FileChannel channel = randomAccessFile.getChannel();
        FileLock fileLock = channel.tryLock();

        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, fileSize);
        buffer.put(new byte[] {65,66,67});
        System.out.println("qqqq");
        buffer.force();
        System.out.println("wwww");
        if (System.getProperty("os.name").toLowerCase().contains("win")) {
            System.out.println("eeee");
            Cleaner cleaner = ((DirectBuffer) buffer).cleaner();
            System.out.println("rrr");
            if (cleaner != null) {
                System.out.println("ttt");
                cleaner.clean();
                System.out.println("yyy");
            }
            System.out.println("uuu");
        }
        System.out.println("iiii");
        buffer = null;  // Allow it to be GCd and the underlying file mapping to go away.
        System.out.println("oooo");
        randomAccessFile.close();
        System.out.println("pppp");
    }

    public final static int getFileSize() {
        return 3;
    }
}
```
  - Running WindowsTest2 using jdk10 on windows, the log prints:
```
qqqq
wwww
eeee
Exception in thread "main" java.lang.NoSuchMethodError: sun.nio.ch.DirectBuffer.cleaner()Lsun/misc/Cleaner;
at WindowsTest2.main(WindowsTest2.java:34)
```
- I ran on windows the unit tests (bitcoinj at dbc4c35) Andreas wrote on bitcoinj master upstream to make sure removing the windows hack works fine and they passed (that does mean it will pass on every windows environment)

So...

- Moving out of jdk8 for bitcoinj seems to be the way to go to solve this problem.
- If we keep developing, testing and building bitcoinj using jdk8 and run it with java 10 we might find other problems like this one. 
- Then we have 2 possible solutions:
  - Add a copy of the sun.* classes to bitcoinj
  - Remove the windows hack and so the need to use sun.* (ie cherry pick what was done on bitcoinj master upstream).
- I will think a bit more about the 2 solutions and will share my conclusions here.



-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/bisq-network/bitcoinj/issues/17#issuecomment-450174590
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.bisq.network/pipermail/bisq-github/attachments/20181227/9f5e4b3b/attachment-0001.html>


More information about the bisq-github mailing list