Possibility for deadlock to occur during IPC call
<p>I have a <code>MainActivity</code> which will issue IPC call to a remote <code>AutoCompleteService</code> service. </p>

<p>During execution of <code>AutoCompleteService</code>'s IPC function, the service will issue another IPC call back to <code>MainActivity</code>.</p>

<h2>MainActivity.java</h2>

<pre><code>// Receive IPC call from AutoCompleteService.
private StockInfoObserver.Stub stockInfoObserver = new StockInfoObserver.Stub() {

    @Override
    public void update(StockInfo stockInfo) throws RemoteException {
        // TODO Auto-generated method stub
        Log.i(TAG, android.os.Process.myPid() + " : MainActivity receive ipc call : " + Thread.currentThread().getId());
    }

};

...
...
...

// Issue IPC call to AutoCompleteService.
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // Test on API.
        try {
            Log.i(TAG, android.os.Process.myPid() + " : MainActivity start issue IPC call to remote service : " + Thread.currentThread().getId());
            // autoCompleteApi.handle will issue IPC call to remote service.
            autoCompleteApi.handle("abc");
            Log.i(TAG, android.os.Process.myPid() + " : MainActivity end issue IPC call to remote service : " + Thread.currentThread().getId());
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

});
</code></pre>

<h2>AutoCompleteService.java</h2>

<pre><code>private AutoCompleteApi.Stub autoCompleteApi = new AutoCompleteApi.Stub() {    
    private List&lt;StockInfoObserver&gt; stockInfoObservers = new ArrayList&lt;StockInfoObserver&gt;();

    @Override
    public void handle(String string) {
        Log.i(TAG, android.os.Process.myPid() + " : AutoCompleteService start receive ipc call : " + Thread.currentThread().getId());
        try {
            for (StockInfoObserver stockInfoObserver : stockInfoObservers) {    
                Log.i(TAG, android.os.Process.myPid() + " : AutoCompleteService start IPC call to MainActivity : " + Thread.currentThread().getId());
                // stockInfoObserver.update will issue IPC call back to MainActivity
                stockInfoObserver.update(null);
                Log.i(TAG, android.os.Process.myPid() + " : AutoCompleteService end IPC call to MainActivity : " + Thread.currentThread().getId());
            }
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.i(TAG, android.os.Process.myPid() + " : AutoCompleteService end receive ipc call : " + Thread.currentThread().getId());
    }

    @Override
    public void attachStockInfoObserver(StockInfoObserver stockInfoObserver)
            throws RemoteException {
        if (stockInfoObservers.contains(stockInfoObserver) == false) {
            stockInfoObservers.add(stockInfoObserver);
        }
    }
};
</code></pre>

<p>My initial expectation is that, <strong>deadlock will occur</strong>. This is due to my observation. When issuing an IPC call, the issuer will only return from IPC call, after the IPC receiver finished its IPC function execution.</p>

<ol>
<li><code>MainActivity</code> issues IPC call to <code>AutoCompleteService</code> through <code>autoCompleteApi.handle</code>.</li>
<li><code>MainActivity</code> will now wait till <code>AutoCompleteService</code> finished its execution.</li>
<li><code>AutoCompleteService</code> issues IPC call to <code>MainActivity</code> through <code>stockInfoObserver.update</code>.</li>
<li><code>AutoCompleteService</code> will now wait till <code>MainActivity</code> finished its execution.</li>
<li>However, <code>MainActivity</code>'s thread is still waiting, there are no thread which will perform <code>update</code> function.</li>
<li>Both processes keep waiting for each others.</li>
</ol>

<p>However, the above doesn't occur. This is the Log I'm getting. Everything just work flawless.</p>

<pre><code>// Log in MainActivity TAG
3930 : MainActivity start issue IPC call to remote service : 1
3930 : MainActivity receive ipc call : 1
3930 : MainActivity end issue IPC call to remote service : 1
</code></pre>

<hr>

<pre><code>// Log in AutoCompleteService TAG
3961 : AutoCompleteService start receive ipc call : 494
3961 : AutoCompleteService start IPC call to MainActivity : 494
3961 : AutoCompleteService end IPC call to MainActivity : 494
3961 : AutoCompleteService end receive ipc call : 494
</code></pre>

<hr>

<p>But I don't really understand. <strong>If the MainActivity thread (with Id 1) is not returning from a function call (<code>autoCompleteApi.handle</code>), how can it "jump" over to execute another function (<code>update(StockInfo stockInfo)</code>)?</strong></p>

<p>I would be expecting <strong>MainActivity receive ipc call</strong> being printed by different thread. Not the thread with Id 1. If not, deadlock should occur.</p>

<p>In case you are interested to try to out, kindly download the complete source code right here : <a href="https://www.dropbox.com/s/8hd7v5acjd213l1/jstock-android2.zip" rel="nofollow">https://www.dropbox.com/s/8hd7v5acjd213l1/jstock-android2.zip</a></p>
