ChallengeWhat is wrong with this code

time to read 2 min | 282 words

Let us assume that we have the following piece of code. It has a big problem in it. The kind of problem that you get called at 2 AM to solve.

Can you find it? (more below)

public static void Main()
{
	while(true)
	{
		srv.ProcessMessages();
		Thread.Sleep(5000);
	}
}

public void ProcessMessages()
{
	try
	{
	   var msgs = GetMessages();
	   byte[] data = Serialize(msgs);
	   var req =  WebRequest.Create("http://some.remote.server");
	   req.Method = "PUT";
	   using(var stream = req.GetRequestStream())
	   {
		   stream.Write(data,0,data.Length);
	   }
	   var resp = req.GetResponse();
	   resp.Close();// we only care that no exception was thrown
	   
	   MarkMessagesAsHandled(msgs); // assume this can't throw 
	}
	catch(Exception)
	{
		// bummer, but never mind,
		// we will get it the next time that ProcessMessages 
		// is called
	}
}

public Message[] GetMessages()
{
    List<Message> msgs = new List<Message>();
    using(var reader = ExecuteReader("SELECT * FROM Messages WHERE Handled = 0;"))
    while(reader.Read())
    {
        msgs.Add( HydrateMessage(reader) );
    }
    return msgs.ToArray();
}

This code is conceptual, just to make the point. It is not real code. Things that you don't have to worry about:

  • Multi threading
  • Transactions
  • Failed database

The problem is both a bit subtle and horrifying. And just to make things interesting, for most scenarios, it will work just fine.

More posts in "Challenge" series:

  1. (04 Jan 2023) what does this code print?
  2. (14 Dec 2022) What does this code print?
  3. (01 Jul 2022) Find the stack smash bug… – answer
  4. (30 Jun 2022) Find the stack smash bug…
  5. (03 Jun 2022) Spot the data corruption
  6. (06 May 2022) Spot the optimization–solution
  7. (05 May 2022) Spot the optimization
  8. (06 Apr 2022) Why is this code broken?
  9. (16 Dec 2021) Find the slow down–answer
  10. (15 Dec 2021) Find the slow down
  11. (03 Nov 2021) The code review bug that gives me nightmares–The fix
  12. (02 Nov 2021) The code review bug that gives me nightmares–the issue
  13. (01 Nov 2021) The code review bug that gives me nightmares
  14. (16 Jun 2021) Detecting livelihood in a distributed cluster
  15. (21 Apr 2020) Generate matching shard id–answer
  16. (20 Apr 2020) Generate matching shard id
  17. (02 Jan 2020) Spot the bug in the stream
  18. (28 Sep 2018) The loop that leaks–Answer
  19. (27 Sep 2018) The loop that leaks
  20. (03 Apr 2018) The invisible concurrency bug–Answer
  21. (02 Apr 2018) The invisible concurrency bug
  22. (31 Jan 2018) Find the bug in the fix–answer
  23. (30 Jan 2018) Find the bug in the fix
  24. (19 Jan 2017) What does this code do?
  25. (26 Jul 2016) The race condition in the TCP stack, answer
  26. (25 Jul 2016) The race condition in the TCP stack
  27. (28 Apr 2015) What is the meaning of this change?
  28. (26 Sep 2013) Spot the bug
  29. (27 May 2013) The problem of locking down tasks…
  30. (17 Oct 2011) Minimum number of round trips
  31. (23 Aug 2011) Recent Comments with Future Posts
  32. (02 Aug 2011) Modifying execution approaches
  33. (29 Apr 2011) Stop the leaks
  34. (23 Dec 2010) This code should never hit production
  35. (17 Dec 2010) Your own ThreadLocal
  36. (03 Dec 2010) Querying relative information with RavenDB
  37. (29 Jun 2010) Find the bug
  38. (23 Jun 2010) Dynamically dynamic
  39. (28 Apr 2010) What killed the application?
  40. (19 Mar 2010) What does this code do?
  41. (04 Mar 2010) Robust enumeration over external code
  42. (16 Feb 2010) Premature optimization, and all of that…
  43. (12 Feb 2010) Efficient querying
  44. (10 Feb 2010) Find the resource leak
  45. (21 Oct 2009) Can you spot the bug?
  46. (18 Oct 2009) Why is this wrong?
  47. (17 Oct 2009) Write the check in comment
  48. (15 Sep 2009) NH Prof Exporting Reports
  49. (02 Sep 2009) The lazy loaded inheritance many to one association OR/M conundrum
  50. (01 Sep 2009) Why isn’t select broken?
  51. (06 Aug 2009) Find the bug fixes
  52. (26 May 2009) Find the bug
  53. (14 May 2009) multi threaded test failure
  54. (11 May 2009) The regex that doesn’t match
  55. (24 Mar 2009) probability based selection
  56. (13 Mar 2009) C# Rewriting
  57. (18 Feb 2009) write a self extracting program
  58. (04 Sep 2008) Don't stop with the first DSL abstraction
  59. (02 Aug 2008) What is the problem?
  60. (28 Jul 2008) What does this code do?
  61. (26 Jul 2008) Find the bug fix
  62. (05 Jul 2008) Find the deadlock
  63. (03 Jul 2008) Find the bug
  64. (02 Jul 2008) What is wrong with this code
  65. (05 Jun 2008) why did the tests fail?
  66. (27 May 2008) Striving for better syntax
  67. (13 Apr 2008) calling generics without the generic type
  68. (12 Apr 2008) The directory tree
  69. (24 Mar 2008) Find the version
  70. (21 Jan 2008) Strongly typing weakly typed code
  71. (28 Jun 2007) Windsor Null Object Dependency Facility