Store and reset SynchronizationContext

fixes #2001

@andreasohlund @indualagarsamy code review please