Work Journal 2013-feb-08

A Bug From the Past

Well it happens. Your test code runs fine, but when you run the full system it crashes. The email integration I have been working on the last couple of days is the kind of legacy code that I created myself. It is the first “full” system I built (really I was seriously a junior at that time, but very keen on learning and doing this “right”) and actually I’m content with it. Anyway part of what I added is the code I wrote about a few days ago. It turns out that the code has a problem. Here’s the relevant code:

public void Send(string fromEmail, string toEmails,
	string emailSubject, string emailBody,
		string attachmentPath )
{
	var mail = new MailMessage(fromEmail,
		toEmails.Replace(';', ','))
	{
		SubjectEncoding = Encoding.UTF8,
		Subject = emailSubject,
		BodyEncoding = Encoding.UTF8,
		Body = emailBody,
		Attachments = {new Attachment(attachmentPath)}
	};
	_smtpClient.Send(mail);
}

When the email has been sent the file that was sent as the attachment is deleted from the disk. When I tested the code in the full running system it turns out that the deletion failed. The problem is that MailMessage keeps the reference to the attached file. Oops. MailMessage is IDisposable, so really I should have wrapped it in a using block anyway.

public void Send(string fromEmail, string toEmails, 
	string emailSubject, string emailBody, 
		string attachmentPath )
{

	using (var mail = new MailMessage(fromEmail, 
		toEmails.Replace(';', ','))
	{
		SubjectEncoding = Encoding.UTF8,
		Subject = emailSubject,
		BodyEncoding = Encoding.UTF8,
		Body = emailBody,
		Attachments = {new Attachment(attachmentPath)}
	})
	{
		_smtpClient.Send(mail);
	}
}

and there was much rejoicing.

Testing in "Production" with AWS

I really like Amazon EC2 as production environment. Not just because [insert the usual cloud buzz words], but because it allows me to clone the production machines, launch the clones, deploy new features on the clones, swap IPs and then test on a setup that is exactly like the production system (when the new features have been deployed). That's what I did today - testing on the "production" system and fixed the bug.


Work Journal – a diary on what I did work/programming related today.