Return-Path: X-Original-To: apmail-mina-dev-archive@www.apache.org Delivered-To: apmail-mina-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6D81260D0 for ; Fri, 29 Jul 2011 11:57:37 +0000 (UTC) Received: (qmail 45094 invoked by uid 500); 29 Jul 2011 11:57:37 -0000 Delivered-To: apmail-mina-dev-archive@mina.apache.org Received: (qmail 44686 invoked by uid 500); 29 Jul 2011 11:57:33 -0000 Mailing-List: contact dev-help@mina.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@mina.apache.org Delivered-To: mailing list dev@mina.apache.org Received: (qmail 44673 invoked by uid 99); 29 Jul 2011 11:57:31 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 29 Jul 2011 11:57:31 +0000 X-ASF-Spam-Status: No, hits=-2001.2 required=5.0 tests=ALL_TRUSTED,RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.116] (HELO hel.zones.apache.org) (140.211.11.116) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 29 Jul 2011 11:57:30 +0000 Received: from hel.zones.apache.org (hel.zones.apache.org [140.211.11.116]) by hel.zones.apache.org (Postfix) with ESMTP id A5E2691EDB for ; Fri, 29 Jul 2011 11:57:09 +0000 (UTC) Date: Fri, 29 Jul 2011 11:57:09 +0000 (UTC) From: "Emmanuel Lecharny (JIRA)" To: dev@mina.apache.org Message-ID: <1974867252.18320.1311940629676.JavaMail.tomcat@hel.zones.apache.org> In-Reply-To: <201737154.17417.1311907689503.JavaMail.tomcat@hel.zones.apache.org> Subject: [jira] [Commented] (DIRMINA-845) ProtocolEncoderOutputImpl isn't thread-safe MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/DIRMINA-845?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13072787#comment-13072787 ] Emmanuel Lecharny commented on DIRMINA-845: ------------------------------------------- Ok, when you send a message, you do it using the thread that was used to process the incoming request. This thread has been selected when the session n which this message has arrived was activated. If you haven't changed anything (like, adding an executor in the chain), then an incoming message for a specific session will *always* use the same thread, so there is no reason a message B can be written before message A, as the thread isn't available before message A is injected into the queue. That's the theory, and trust me, it works. Now, if I don't have the code you are playing with, I won't be able to explain why you see some concurrent issues. > ProtocolEncoderOutputImpl isn't thread-safe > ------------------------------------------- > > Key: DIRMINA-845 > URL: https://issues.apache.org/jira/browse/DIRMINA-845 > Project: MINA > Issue Type: Bug > Components: Filter > Affects Versions: 2.0.4 > Reporter: Ilya Ivanov > > ProtocolEncoderOutputImpl uses ConcurrentLinkedQueue and at first look it seems to be thread-safe. But really concurrent execution of flush method isn't thread-safe (and write-mergeAll also). > E.g. in RTMP several channels multiplexed in single connection. According protocol specification it's possible to write to different channels concurrently. But it doesn't work with MINA. > I've synchronized channel writing, but it doesn't prevent concurrent run of flushing (in 2.0.4 it's done directly in ProtocolCodecFilter.filterWrite, but ProtocolEncoderOutputImpl.flush has the same problem). > Here the fragment of flushing code: > while (!bufferQueue.isEmpty()) { > Object encodedMessage = bufferQueue.poll(); > > if (encodedMessage == null) { > break; > } > // Flush only when the buffer has remaining. > if (!(encodedMessage instanceof IoBuffer) || ((IoBuffer) encodedMessage).hasRemaining()) { > SocketAddress destination = writeRequest.getDestination(); > WriteRequest encodedWriteRequest = new EncodedWriteRequest(encodedMessage, null, destination); > nextFilter.filterWrite(session, encodedWriteRequest); > } > } > Suppose original packets sequence is A, B, ... > Concurrent run of flushing may proceed as following: > thread-1: Object encodedMessage = bufferQueue.poll(); // gets A packet > thread-2: Object encodedMessage = bufferQueue.poll(); // gets B packet > ... > thread-2: nextFilter.filterWrite(...); // writes B packet > thread-1: nextFilter.filterWrite(...); // writes A packet > so, resulting sequence will B, A > It's quite confusing result especially when documentation doesn't contain any explanation about such behavior. -- This message is automatically generated by JIRA. For more information on JIRA, see: http://www.atlassian.com/software/jira