From owner-freebsd-hackers@freebsd.org Wed Oct 11 14:48:18 2017 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id D4AEDE2E2B5 for ; Wed, 11 Oct 2017 14:48:18 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 6C7FE6AC46 for ; Wed, 11 Oct 2017 14:48:18 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id v9BEm9Qm073367 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 11 Oct 2017 17:48:10 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua v9BEm9Qm073367 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id v9BEm9cN073366; Wed, 11 Oct 2017 17:48:09 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Wed, 11 Oct 2017 17:48:09 +0300 From: Konstantin Belousov To: Karl Pielorz Cc: freebsd-hackers@freebsd.org Subject: Re: fprintf - threadsafe? - i.e. with process linked against '-pthread'? Message-ID: <20171011144809.GA95911@kib.kiev.ua> References: <03DA6274A199550235DC7351@[10.12.30.106]> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <03DA6274A199550235DC7351@[10.12.30.106]> User-Agent: Mutt/1.9.1 (2017-09-22) X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on tom.home X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 Oct 2017 14:48:18 -0000 On Wed, Oct 11, 2017 at 10:13:35AM +0100, Karl Pielorz wrote: > > Hi, > > I have a number of 10.3-R amd64 boxes which runs a heavily threaded > process. This is linked against '-pthread' - and compiles / runs fine. > > Using 'fprintf' to log data to a file - it sometimes doesn't complete > writing the line - e.g. literally in code: > > fprintf( fd, "The quick brown %s jumped over the slow lazy animal\n", > animal ); > > Will sometimes result in: > > " > The quick brown fox ju" > > Being written to the file. > > > Presumably (and from what I can see) fprintf is 'thread safe'? - And it > also appears multiple threads could write to a single file using it (i.e. > it provides for atomic writes so lines won't intermingle - the lines > written don't seem to intermingle). > > The process doesn't crash - but I can't understand why / how frpintf could > either stop, or get stopped 'mid way' through? > > e.g. If a signal occurred would it complete the write to file? > > This only happens very, very occasionally (one fprintf out of many > millions, with hundreds of threads running). > > Just a bit stumped as to what to try looking at to fix / debug the problem > - if anyone has any suggestions, or further reading I can look at. Does the program use cancellation ? If yes, it might be an issue solved by the r321074. Otherwise, you need to debug the program. I usually use ktrace for start, but if the event is rare and program intensively issues syscalls, you would need to develop some ad-hoc tecnhique.