mina-kang-syd / javapns

Automatically exported from code.google.com/p/javapns
0 stars 0 forks source link

Race Condition when sending large amount of Push Notifications #98

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi all

We are using your Framework in Production to send large amounts of Push 
Notifications. We had a strange behaviour that suddenly the whole processing of 
sending the messages stopped.

With the help of a heap dump we saw that a thread in your framework was 
waiting/blocking.

"task-scheduler-4" prio=10 tid=0x00002aaaf88b3000 nid=0x741e in Object.wait() 
[0x0000000042d12000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00002aaab3db2f18> (a java.lang.Object)
        at java.lang.Object.wait(Object.java:485)
        at javapns.notification.transmission.NotificationThreads.waitForAllThreads(NotificationThreads.java:344)
        - locked <0x00002aaab3db2f18> (a java.lang.Object)
        at javapns.notification.transmission.NotificationThreads.waitForAllThreads(NotificationThreads.java:363)
        at javapns.Push.payloads(Push.java:272)
        at com.namics.wetteralarm.service.ApplePushNotificationServiceImpl.sendPushNotifications(ApplePushNotificationServiceImpl.java:87)

The race condition occurs in the method threadFinished in the class 
NotificationThreads. The operation threadsRunning-- ist not atomic and this can 
lead to the race condition between counting down and compare it with 0.

--> http://www.informit.com/guides/content.aspx?g=java&seqNum=248

So our solution was to define the method as synchronized:

protected synchronized void threadFinished(NotificationThread 
notificationThread) {
        threadsRunning--;
        if (threadsRunning == 0) {
            if (listener != null)
                listener.eventAllThreadsFinished(this);
            try {
                synchronized (finishPoint) {
                    finishPoint.notifyAll();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

Maybe you could fix this issue in a further release? Am i the first one who 
reported this error or did you already had some thread issues?

regards
angela

Original issue reported on code.google.com by angela.s...@gmail.com on 11 Jan 2012 at 7:52

GoogleCodeExporter commented 8 years ago
This issue is already fixed in the trunk.  Thank you!

Original comment by sype...@gmail.com on 11 Jan 2012 at 4:47

GoogleCodeExporter commented 8 years ago
the 2.2APNS have still bean occurring on my system.
Like this below.
--------------------------------------------------------------------------------
-
Deadlock Detection:

No deadlocks found.

none
null_check
null_assert
range_check
class_check
array_check
intrinsic
bimorphic
unloaded
uninitialized
unreached
unhandled
constraint
div0_check
age
predicate
loop_limit_check
Thread 22448: (state = BLOCKED)
 - java.lang.Object.wait(long) @bci=0 (Interpreted frame)
 - java.util.TimerThread.mainLoop() @bci=201, line=552 (Interpreted frame)
 - java.util.TimerThread.run() @bci=1, line=505 (Interpreted frame)

Locked ownable synchronizers:
    - None

Thread 22440: (state = BLOCKED)

Locked ownable synchronizers:
    - None

Thread 22439: (state = BLOCKED)
 - java.lang.Object.wait(long) @bci=0 (Interpreted frame)
 - java.lang.ref.ReferenceQueue.remove(long) @bci=44, line=135 (Interpreted frame)
 - java.lang.ref.ReferenceQueue.remove() @bci=2, line=151 (Interpreted frame)
 - java.lang.ref.Finalizer$FinalizerThread.run() @bci=3, line=177 (Interpreted frame)

Locked ownable synchronizers:
    - None

Thread 22438: (state = BLOCKED)
 - java.lang.Object.wait(long) @bci=0 (Interpreted frame)
 - java.lang.Object.wait() @bci=2, line=503 (Interpreted frame)
 - java.lang.ref.Reference$ReferenceHandler.run() @bci=46, line=133 (Interpreted frame)

Locked ownable synchronizers:
    - None

Thread 22432: (state = BLOCKED)
 - java.lang.Object.wait(long) @bci=0 (Interpreted frame)
 - java.lang.Object.wait() @bci=2, line=503 (Interpreted frame)
 - javapns.notification.transmission.NotificationThreads.waitForAllThreads() @bci=11, line=351 (Interpreted frame)
 - javapns.notification.transmission.NotificationThreads.waitForAllThreads(boolean) @bci=1, line=370 (Interpreted frame)
 - javapns.Push.payload(javapns.notification.Payload, java.lang.Object, java.lang.String, boolean, int, java.lang.Object) @bci=59, line=212 (Interpreted frame)
 - jp.ameba.sg2.apns.core.service.impl.ApnsServiceImpl.sendMessage(java.lang.String[], java.lang.String, java.util.Map, int, jp.ameba.sg2.apns.core.bean.model.ApnsGameModel, boolean) @bci=63, line=303 (Interpreted frame)
 - jp.ameba.sg2.apns.batch.service.impl.BatchServiceImpl.sendMessage(java.lang.String[], java.lang.String, java.util.Map, int, jp.ameba.sg2.apns.core.bean.model.ApnsGameModel, java.lang.String, boolean) @bci=13, line=71 (Compiled frame)
 - jp.ameba.sg2.apns.batch.service.impl.BatchServiceImpl.sendMessageAsList(java.lang.String, java.lang.String, java.util.Map, int, java.lang.String, boolean) @bci=184, line=158 (Interpreted frame)
 - jp.ameba.sg2.apns.batch.main.BatchMain.main(java.lang.String[]) @bci=369, line=78 (Interpreted frame)

Locked ownable synchronizers:
    - None
--------------------------------------------------------------------------------
-----

Original comment by minzia...@gmail.com on 13 May 2013 at 5:52