Announcement Announcement Module
Collapse
No announcement yet.
Is new RestTemplate() thread safe? (Android) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is new RestTemplate() thread safe? (Android)

    Hello all,

    I just wanted to check: is the constructor for RestTemplate thread safe? I just fixed a very bad bug in our Android application that happened when two threads tried to create instance of RestTemplate at approximately the same time. The application would crash with a segmentation fault and dump the user back into the desktop, with no "Force Close" message or anything. I just fixed this by surrounding the new RestTemplate(factory) call in a synchronized method. Boom, no crashes . Is this a known issue or could something in my code be causing this??

    Thanks!

    Here's a sample crash from ICS (it crashes on all versions though):

    01-25 17:39:01.285: I/DEBUG(1741): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
    01-25 17:39:01.285: I/DEBUG(1741): Build fingerprint: 'google/soju/crespo:4.0.3/IML74K/239410:user/release-keys'
    01-25 17:39:01.285: I/DEBUG(1741): pid: 5263, tid: 5281
    01-25 17:39:01.285: I/DEBUG(1741): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000014
    01-25 17:39:01.285: I/DEBUG(1741): r0 00000000 r1 01753d50 r2 00000021 r3 00000019
    01-25 17:39:01.285: I/DEBUG(1741): r4 00000000 r5 405477a4 r6 50df2a58 r7 01753c48
    01-25 17:39:01.285: I/DEBUG(1741): r8 50df2bb0 r9 405477a0 10 4c5537f0 fp 50df2c24
    01-25 17:39:01.285: I/DEBUG(1741): ip 0000000d sp 50df2a08 lr 404a2eab pc 4049b546 cpsr 00000030
    01-25 17:39:01.285: I/DEBUG(1741): d0 00000a2900000a00 d1 00000a3100000a00
    01-25 17:39:01.285: I/DEBUG(1741): d2 00000a3700000a11 d3 00000a3a00000a00
    01-25 17:39:01.285: I/DEBUG(1741): d4 4bb702784bb70240 d5 4be22cf84bb702b0
    01-25 17:39:01.285: I/DEBUG(1741): d6 4be22d684be22d30 d7 4be22dd84be22da0
    01-25 17:39:01.285: I/DEBUG(1741): d8 0000000000000000 d9 0000000000000000
    01-25 17:39:01.285: I/DEBUG(1741): d10 0000000000000000 d11 0000000000000000
    01-25 17:39:01.285: I/DEBUG(1741): d12 0000000000000000 d13 0000000000000000
    01-25 17:39:01.285: I/DEBUG(1741): d14 0000000000000000 d15 0000000000000000
    01-25 17:39:01.285: I/DEBUG(1741): d16 401195c800000001 d17 ff131313ff131313
    01-25 17:39:01.285: I/DEBUG(1741): d18 0707070703030303 d19 ff131313ff131313
    01-25 17:39:01.285: I/DEBUG(1741): d20 0100010001000100 d21 0100010001000100
    01-25 17:39:01.285: I/DEBUG(1741): d22 ff131313ff131313 d23 ff00130013001300
    01-25 17:39:01.285: I/DEBUG(1741): d24 ff00130013001300 d25 ff00130013001300
    01-25 17:39:01.285: I/DEBUG(1741): d26 0100010001000100 d27 0100010001000100
    01-25 17:39:01.285: I/DEBUG(1741): d28 0100010001000100 d29 0100010001000100
    01-25 17:39:01.285: I/DEBUG(1741): d30 0001000000010000 d31 0001000000010000
    01-25 17:39:01.285: I/DEBUG(1741): scr 60000012
    01-25 17:39:01.484: I/ActivityManager(151): No longer want com.android.providers.calendar (pid 5101): hidden #16
    01-25 17:39:01.512: I/DEBUG(1741): #00 pc 0004a546 /system/lib/libicuuc.so (_ZN6icu_4610UnicodeSet7compactEv)
    01-25 17:39:01.512: I/DEBUG(1741): #01 pc 00051ea6 /system/lib/libicuuc.so (uset_compact_46)
    01-25 17:39:01.512: I/DEBUG(1741): #02 pc 000885ec /system/lib/libicuuc.so
    01-25 17:39:01.512: I/DEBUG(1741): #03 pc 0007d644 /system/lib/libicuuc.so
    01-25 17:39:01.512: I/DEBUG(1741): #04 pc 0007d84c /system/lib/libicuuc.so
    01-25 17:39:01.512: I/DEBUG(1741): #05 pc 00080126 /system/lib/libicuuc.so (ucnv_open_46)
    01-25 17:39:01.512: I/DEBUG(1741): #06 pc 00014020 /system/lib/libnativehelper.so
    01-25 17:39:01.516: I/DEBUG(1741): #07 pc 0001ebf0 /system/lib/libdvm.so (dvmPlatformInvoke)
    01-25 17:39:01.516: I/DEBUG(1741): #08 pc 00058c30 /system/lib/libdvm.so (_Z16dvmCallJNIMethodPKjP6JValuePK6MethodP6Thread)
    01-25 17:39:01.516: I/DEBUG(1741): code around pc:
    01-25 17:39:01.516: I/DEBUG(1741): 4049b524 ff32f7ff 29009901 af5cf73f f44f68a0 ..2....)?.\..hO.
    01-25 17:39:01.516: I/DEBUG(1741): 4049b534 69231188 f8431c42 60a21020 bf00e799 ..#iB.C. ..`....
    01-25 17:39:01.516: I/DEBUG(1741): 4049b544 6943b570 b10b4604 bd704620 28006ac0 p.Ci.F.. Fp..j.(
    01-25 17:39:01.516: I/DEBUG(1741): 4049b554 f894d1fa f0155030 d1f50501 b11069a0 ....0P.......i..
    01-25 17:39:01.516: I/DEBUG(1741): 4049b564 fbb4f021 68a561a5 428d68e1 2d00daec !....a.h.h.B...-
    01-25 17:39:01.516: I/DEBUG(1741): code around lr:
    01-25 17:39:01.516: I/DEBUG(1741): 404a2e88 bd10fa63 f7f8b510 bd10fc7d f7f8b510 c.......}.......
    01-25 17:39:01.516: I/DEBUG(1741): 404a2e98 bd10fd19 f7f8b510 bd10fa2f f7f8b510 ......../.......
    01-25 17:39:01.516: I/DEBUG(1741): 404a2ea8 bd10fb4d f7f8b510 bd10fcb1 f7f9b510 M...............
    01-25 17:39:01.516: I/DEBUG(1741): 404a2eb8 bd10fb01 f7f8b510 bd10fc8f b08ab570 ............p...
    01-25 17:39:01.516: I/DEBUG(1741): 404a2ec8 460eac01 f1b24613 bf143fff 21012100 ...F.F...?...!.!
    01-25 17:39:01.516: I/DEBUG(1741): stack:
    01-25 17:39:01.519: I/DEBUG(1741): 50df29c8 50df29e4
    01-25 17:39:01.519: I/DEBUG(1741): 50df29cc 4049aeef /system/lib/libicuuc.so
    01-25 17:39:01.519: I/DEBUG(1741): 50df29d0 01795148 [heap]
    01-25 17:39:01.519: I/DEBUG(1741): 50df29d4 50df2a04
    01-25 17:39:01.519: I/DEBUG(1741): 50df29d8 01795148 [heap]
    01-25 17:39:01.519: I/DEBUG(1741): 50df29dc 4049b245 /system/lib/libicuuc.so
    01-25 17:39:01.519: I/DEBUG(1741): 50df29e0 01795148 [heap]
    01-25 17:39:01.519: I/DEBUG(1741): 50df29e4 00000000
    01-25 17:39:01.519: I/DEBUG(1741): 50df29e8 01795148 [heap]
    01-25 17:39:01.519: I/DEBUG(1741): 50df29ec 405477a4
    01-25 17:39:01.519: I/DEBUG(1741): 50df29f0 50df2a58
    01-25 17:39:01.519: I/DEBUG(1741): 50df29f4 01753c48 [heap]
    01-25 17:39:01.519: I/DEBUG(1741): 50df29f8 50df2bb0
    01-25 17:39:01.519: I/DEBUG(1741): 50df29fc 4049c4ab /system/lib/libicuuc.so
    01-25 17:39:01.519: I/DEBUG(1741): 50df2a00 df0027ad
    01-25 17:39:01.519: I/DEBUG(1741): 50df2a04 00000000
    01-25 17:39:01.519: I/DEBUG(1741): #00 50df2a08 00000000
    01-25 17:39:01.519: I/DEBUG(1741): 50df2a0c 405477a4
    01-25 17:39:01.519: I/DEBUG(1741): 50df2a10 50df2a58
    01-25 17:39:01.531: I/DEBUG(1741): 50df2a14 404a2eab /system/lib/libicuuc.so
    01-25 17:39:01.531: I/DEBUG(1741): #01 50df2a18 00000000
    01-25 17:39:01.531: I/DEBUG(1741): 50df2a1c 404d95f1 /system/lib/libicuuc.so
    01-25 17:39:02.562: I/wpa_supplicant(279): wlan0: WPA: Group rekeying completed with 00:26:5a:fe:d8:00 [GTK=TKIP]
    01-25 17:39:03.184: I/BootReceiver(151): Copying /data/tombstones/tombstone_08 to DropBox (SYSTEM_TOMBSTONE)

  • #2
    Rest Template delegates to the ThreadSafeClientConnManager when using the HttpComponentsClientHttpRequestFactory or HttpURLConnection when using SimpleClientHttpRequestFactory. As the name implies, the Http Components client is supposed to be thread safe, however HttpURLConnection is NOT thread safe, as stated in the android reference. Rest Template is not adding any facility for thread safety on top of these.

    Comment


    • #3
      RestTemplate constructor and thread safety

      Hi Roy,

      I think I am seeing the same problem with the RestTemplate(boolean) constructor. I am wondering if there is a way to guarantee that there won't be a segmentation fault. I tried using the constructor passing HttpComponentsClientHttpRequestFactory and I am seeing a similar segmentation fault.

      Originally posted by Roy Clarkson View Post
      Rest Template delegates to the ThreadSafeClientConnManager when using the HttpComponentsClientHttpRequestFactory or HttpURLConnection when using SimpleClientHttpRequestFactory. As the name implies, the Http Components client is supposed to be thread safe, however HttpURLConnection is NOT thread safe, as stated in the android reference. Rest Template is not adding any facility for thread safety on top of these.

      Comment

      Working...
      X