bug 855716 - safely handle tmpdir and use lock correctly

@lonnen can you take a look at this please?