DeadObjectExceptionとTimeout executing service
MixiClientの機能拡張実装している際に以下のようなエラーが出ました。
・04-29 15:50:59.641: WARN/ActivityManager(74): Timeout executing service: ServiceRecord{4368f438 jp.co.haw.mixiclient/.proxy.HTTPRequestProxy}
・DeadObjectException
どちらもAlartManagerからServiceコールした際に、httpリクエストやtextのparse、DBへのinsert処理を行った際に発生しました。(処理時間は20秒~30秒)
DeadObjectExceptionはその後何故か再現しなくなったため、なんともいえないのですがTimeoutの方は再現性があったため、いろいろと調べて見たところ以下のようなメールを発見。
The activity manager assumes that your code has died, because it did
not return from the broadcast receiver in a “reasonable” amount of
time. In general, receivers should execute very very quickly, and any
long-term operations (such as *any* network operations) should be on
their own thread(s).
呼び出し元のActivityManagerは応答を早く返してくれないと死んだものとして取り扱うため、早めに応答を返す必要があり、通信処理などの時間のかかる処理は別スレッドを立てて行うべきだとのこと。
今回の現象では、「Timeout executing service」メッセージが出た後の処理は継続されていなかったため、おそらく応答が無かったときの「死んだものとして見なす」というのは処理が継続していようとしてなかろうと応答が無かったらそのスレッド(?)をkillしているのではないかと思います。
実処理を別スレッドに行うようにするとTimeoutしなくなりました。
BroadCastReceiverのOnReceive()メソッドにも以下のように書かれています。
so you should never perform long-running operations in it (there is a timeout of 10 seconds that the system allows before considering the receiver to be blocked and a candidate to be killed).
長くても10秒くらいがベター、ということでしょうね。
それにしても気になるのはDeadObjectException。
プロセスを隔てたものにアクセスする際(Service呼び出しやContentProvider経由など)はthrowするExceptionとしてDeadObjectExceptionが指定されているので、
このメソッドの発生条件と発生した際の対応は気にかけておく必要があるのですが、
google先生に聞いてもあまり有用な情報が見つからず一旦ペンディング状態。
何か判明したら追記していこうと思います。